Finding [x,y] - coordinate from peaks in plot

7 views (last 30 days)
Hello! I was searching for a solution, but wasn't able to find any satisyfing answer, since every method I found, is for a specifically problem.
Describing my case: By reading a signal, applying a fft on it, I am generating values inside my for loop, which are being plotted in a certain time section. Now, i used the findpeaks method to show my peaks. Now i want to find the x-y coordiantes from these peaks, which are above 50000. What I mean, is that i am showing only peaks from plot, which are above 50000 and I want the coordinates from those peaks. Is there a method, how to pull this off? Or maybe even a solution. I inserted a image here
clear;
clc;
%%MATLAB
%%read file
%_________________________________________
[y,fs]=audioread('Undertale - Megalovania.wav');
% audioread = read wav -file
% y = contains the audio signal
% fs = 44100
% 'UnchainMyHeart' = name of the wav-file
%_________________________________________
%%PARAMETER FOR STFT
%_________________________________________
t_seg=0.03; % length of segment in ms
fftlen = 4096; %FFT-Points
% Defining size of frequency bands
f_low= 1:200; %lower frequencies
f_medium= 201:600; %medium frequencies
f_high= 601:1000; %higher frequencies
%__________________________________________
%%CODE
segl =floor(t_seg*fs);
windowshift=segl/2;
% defining the size of the window shift
window=hann(segl);
% apply hann function on segment length (30 ms)
window=window.';
% transpose vector
si=1;
% defining start index
ei=segl;
% defining end index
N=floor( length(y)/windowshift - 1);
% Calculates the number, how often the window has to shift
% until to length of the audio signal
f1=figure;
% Generating new window
f=0:1:fftlen-1;
f=f/fftlen*fs;
% defining frequency vector
Ya=zeros(1,fftlen);
ValuesOfYc = NaN(1,N);
x =(1:N)*windowshift/fs;
% defining x-axis
for m= 1:1:N
y_a = y(si:ei);
% a segment is taken out from audio signal length(30ms)
y_a= y_a.*window;
% multiplying segment with window (hanning)
Ya=fft(y_a, fftlen);
% Applying fft on segment
Yb=abs(Ya(1:end/2)).^2;
% Squaring the magnitudes from one-sided spectrum
drawnow; % Updating the graphical values
figure(f1);
% Showing the power values
%%frequency bands
y_low = Yb(f_low); % LOW frequency spectrum
Yc=sum(y_low);
% Summing all the power values from one frequency spectrum together
% so you get one power value from one spectrum
ValuesOfYc(m) = Yc;
%Output values are being saved here, which are generated from the for
%loop
% m = start variable from for loop
[pks0, locs0] = findpeaks(ValuesOfYc);
subplot(2,1,1)
p=plot(x,ValuesOfYc,'r-' ,x(locs0(pks0>=50000)), pks0(pks0>=50000),'ob');
p(1).LineWidth =0.5;
xlabel('time (Audio length)')
ylabel('Power')
grid on
si=si+windowshift;
% Updating start index
ei=ei+windowshift;
% Updating end index
end

Answers (1)

Michael Abboud
Michael Abboud on 24 Feb 2017
You mention that you want to find the [x,y] coordinates of the peaks in your signal. By [x,y], I assume you mean the [time,power] pair from your plot for each of the peaks.
You can accomplish this using the "findpeaks" function, similar to how you have above. In particular, you can use something similar to the following syntax, where the 'MinPeakHeight' flag will automatically eliminate any peaks lower than some given value.
[peakY,peakX] = findpeaks(y,x,'MinPeakHeight',50000);
For more information on this function, or some examples of other ways to use it, you can refer to the following page which provides several detailed examples:

Tags

Community Treasure Hunt

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

Start Hunting!