Gaussian shape for 'findpeaks' function?

7 views (last 30 days)
Fercho_Sala
Fercho_Sala on 30 Apr 2021
Answered: Nipun on 6 Jun 2024
Hello everyone, probably this is an easy question…. for the ‘findpeaks’ function how can I do its plot looks like a Gaussian shape and modify the markers style? Thanks. Here is the code:
ax17=subplot(1,2,2);
[pks4,locs4,widths4,proms4] = findpeaks(pws4(:,end),y,'MinPeakHeight',25);
findpeaks(pws4(:,end),y,'Annotate','extents','WidthReference','halfprom'); % Gaussian shape? how ??
text(locs4+0.5,pks4,num2str((1:numel(pks4))'));
legend('Filtered Data','Peak','Prominence','Width','FontSize',5);
ax17.YAxisLocation = 'right';
ax17.XDir = 'reverse';
ax17.XGrid = 'on';
ax17.YGrid = 'off';
title ('Time (20:30 - 21:00)');
ylabel('Layering identification','FontSize',9,'FontName','Arial','color','default');
xlabel('Altitude/km','FontSize',9,'color','default');
yticklabels('');
xlim(ax17,[75 95]);
ylim(ax17,[15 19]);
camroll(-90)
%typo edited
  7 Comments
Fercho_Sala
Fercho_Sala on 30 Apr 2021
Edited: Fercho_Sala on 30 Apr 2021
@Mathieu NOE yes, the reason is that there is not a plot for the left side, this is another different code... and of course the limits must be resized, the question is more about to represent the line (data) in a Gaussian shape, it is possible?, Sir @Stephen Cobeldick any comment?
Mathieu NOE
Mathieu NOE on 1 May 2021
hello
there are some points to be clarified :
  • pws4 has 7 columns , but seems you are only interested in the last column - correct ? : pws4(:,end)
  • this line does not work as the data has no peak above 25 (max value is about 22)
[pks4,locs4,widths4,proms4] = findpeaks(pws4(:,end),y,'MinPeakHeight',25);
  • the next line works but I don't understand what you are trying to achieve - what does the gaussian shape stuff mean ? you would like to fit a king of gaussian pulse to the data ? and take the gaussain peak value coordinates ?
  • basically this is what this line gives :
but then I wondered if the question was about a technique to shape the data to give them a king of gaussian shape
if that is the question see the output of these two smoothing filters; the sgolay performs better IMHO
code :
% smoothing filters (makes the data have a pseudo gaussian shape)
figure(2)
tmp = pws4(:,end);
tmps = medfilt1(tmp, 25,'truncate');
tmpss = sgolayfilt(tmp,2,71);
plot(y,tmp,y,tmps,y,tmpss);legend('Raw','Smoothed medfilt1','Smoothed sgolayfilt');
title('Data Smoothed');

Sign in to comment.

Answers (1)

Nipun
Nipun on 6 Jun 2024
Hi Fercho,
I understand that you want to plot the peaks found using the "findpeaks" function in a way that resembles a Gaussian shape and modify the marker style. Here's how you can do it:
  1. Fit a Gaussian to the peaks.
  2. Modify the markers in the plot.
Here is the modified version of your code:
% Example data
y = linspace(0, 100, 1000);
pws4 = randn(1000, 1) + 50*exp(-(y-50).^2/(2*10^2)); % Example data with peaks
% Find peaks
[pks4, locs4, widths4, proms4] = findpeaks(pws4, y, 'MinPeakHeight', 25);
% Fit Gaussian to each peak
gaussEqn = 'a*exp(-((x-b)/c)^2)'; % Gaussian function
figure;
ax17 = subplot(1,2,2);
hold on;
% Plot original data
plot(y, pws4, 'DisplayName', 'Filtered Data');
% Plot peaks with Gaussian fit
for i = 1:length(pks4)
% Fit Gaussian
x_peak = y(locs4(i)-10:locs4(i)+10);
y_peak = pks4(i) * exp(-((x_peak-locs4(i))./widths4(i)).^2);
plot(x_peak, y_peak, '--r', 'LineWidth', 1.5); % Gaussian fit
% Plot peak markers
plot(locs4(i), pks4(i), 'xk', 'MarkerSize', 10, 'LineWidth', 2); % Marker style
end
% Annotate peaks
text(locs4 + 0.5, pks4, num2str((1:numel(pks4))'));
% Set plot properties
legend('Filtered Data', 'Gaussian Fit', 'Peak');
ax17.YAxisLocation = 'right';
ax17.XDir = 'reverse';
ax17.XGrid = 'on';
ax17.YGrid = 'off';
title('Time (20:30 - 21:00)');
ylabel('Layering identification', 'FontSize', 9, 'FontName', 'Arial', 'color', 'default');
xlabel('Altitude/km', 'FontSize', 9, 'color', 'default');
yticklabels('');
xlim(ax17, [75 95]);
ylim(ax17, [15 19]);
camroll(-90);
In this code:
  1. We fit a Gaussian function to each peak and plot it.
  2. We modify the marker style for the peaks.
Hope this helps.
Regards,
Nipun

Products


Release

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!