从图中的峰找到[x,y] - 坐标

时间:2017-02-28 12:39:25

标签: matlab

我正在寻找一个解决方案,但由于我发现的每一种方法都是针对特定问题的,因此无法找到任何令人满意的答案。

描述我的情况: 通过读取信号并在其上应用fft,我在for循环中生成值,这些值在特定时间段内绘制。我用圆圈标记了50000以上的值。现在,我试图找出哪个时间和功率,是标记值,这是50000以上。基本上我试图找到x-y坐标。我在寻找,但我找不到任何有用的东西。也许我一直看错了。最后,还有更多。

这是我的代码和情节。

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);
    ValuesOfYd = NaN(1,N);
    ValuesOfYe = 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;

    %plot(x,ValuesOfYc,'marker','s');
    %[ym,ix]=find(sign(diff(ValuesOfYc))==-500,50000,'first');
    %plot((x(ix) ValuesOfYc(ix), [1 ,0 ,0]);

    xlabel('time (Audio length)')
    ylabel('Power')
    grid on


    si=si+windowshift; 
    % Updating start index 
    ei=ei+windowshift; 
    % Updating end index


end

for o= 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_medium = Yb(f_medium); % MEDIUM frequency spectrum
    y_high = Yb(f_high); % HIGH frequency spectrum

    Yd=sum(y_medium);
    Ye=sum(y_high);
    % Summing all the power values from one frequency spectrum together
    % so you get one power value from one spectrum 
    ValuesOfYd(o) = Yd;
    ValuesOfYe(o) = Ye;
    %Output values are being saved here, which are generated from the for
    %loop
    % m = start variable from for loop

    [pks1, locs1] = findpeaks(ValuesOfYd); 
    [pks2, locs2] = findpeaks(ValuesOfYe);

    subplot(2,1,2)
    p=plot(x, ValuesOfYd,'g-', x(locs1(pks1>=400)), pks1(pks1>=400),'ro',...
        x, ValuesOfYe,'b-', x(locs2(pks2>=165)), pks2(pks2>=165),'ro' );
    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  

enter image description here

enter image description here

我并没有命令你给我解决方案或类似的东西,而是要求正确的步骤或建议。顺便说一句,我尝试使用这样的代码方法,并自己应用它,但它没有很好地解决。 这是我试图使用的那个:

x = 0:30:360;
y = sin(x)+rand(size(x));
line(x,y,'marker','s');
[ym,ix]=find(sign(diff(y))==-1,1,'first');
line(x(ix),y(ix),'marker','s','markerfacecolor', [0.56 ,0.5 ,0.5]); 

0 个答案:

没有答案