How to find the x&y co-ordinates of the highest peak and storing the data into a variable

5 views (last 30 days)
Firstly, I apologize if this may seem like a very basic question but I'm just beginning to learn Matlab and I've encountered a problem that I couldn't seem to find the solution to online.
I performed the fft of a signal and plotted the graph of amplitude vs frequency. I also used a Savitsky Golay filter to smoothen the plot so it would be easier.
My problem is, I want to find the X and Y co-ordinates of the largest peak and store the data in a variable. The location of the peak is shown in the figure that is attached. I tried to find a way doing it with the findpeaks() function but did not succeed. I may have been using it wrong though.
I've also added the code of what I could manage to do so far.
num = xlsread('C:\UTwente\Q4\Structural Health and Condition monitoring\Case Roadbridge (Zwartewaterbrug)\00001 Cars 03-23-17 09.29.07 AM.xlsx','Measurement data');
sig = num(:,16);
sig = sig - mean(sig); % Remove d-c Offset
L = length(sig);
Fs = 1000; % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
FTsig = fft(sig)/L;
Fv = linspace(0, 1, fix(length(FTsig)/2)+1)*Fn; % Frequency Vector
Iv = 1:length(Fv); % Index Vector
FTsiga = double(abs(FTsig(Iv))*2); % Truncate, Magnitude, Convert To Double
sgf_sm = sgolayfilt(FTsiga, 5, 501); % Create ‘sgolayfilt’ Filtered FFT
figure(1)
plot(Fv, FTsiga)
hold on
plot(Fv, sgf_sm, '-r', 'LineWidth',2)
hold off
grid
xlabel('Frequency')
ylabel('Amplitude')
legend('Original Spectrum', 'Smoothed & Filtered Spectrum')
  1 Comment
Mark Saad
Mark Saad on 13 Jun 2018
Edited: Mark Saad on 13 Jun 2018
I think findpeaks() should work.
[pks, locs] = findpeaks(sgf_sm);
pks stores the y values of all the peaks, and locs stores the indices of the x values of the peaks, so if you want to get the first peak, you could do
x = Fv(locs(1));
y = pks(1);
plot(x,y);

Sign in to comment.

Accepted Answer

Adam
Adam on 13 Jun 2018
Edited: Adam on 13 Jun 2018
If you just want the highest peak can't you just use
doc max
? with the 2 output syntax to get the index which you can then pass to your frequency vector e.g.
[~, idx] = max( sgf_sm );
peakfreq = Fv( idx );

More Answers (1)

Brett Bodamer
Brett Bodamer on 13 Jun 2018
Try using the max function on your amplitude data. Max has a second output option, which outputs the index of the maximum value in the input vector. Then use that second output to index into your frequency data. You should now have the X and Y coordinates of the highest peak in the plot as well as the indices of those coordinates within your amplitude and frequency vectors.

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!