function PowerSpectrum=PlotFrequencySpectrum(xx,yy,plotmode,XMODE,LabelPeaks) % Computes the power spectrum of x,y with optional linear/log plotting. % plotmode: =0: no plot; =1:linear, =2:semilog X, =3:semilog Y; =4: log-log) % XMODE: =0 for frequency Spectrum (x is frequency); =1 for periodogram (x is time). % If LabelPeaks = 1, uses findpeaksx to locate and label frequency of peaks % Change peak detection parameters in lines 51-54. % Version 2. (c) Tom O'Haver 2017. % % EXAMPLE 1: % x=[0:.01:2*pi]'; % y=sin(200*x)+randn(size(x)); % subplot(2,1,1); % plot(x,y); % subplot(2,1,2); % FrequencySpectrum=PlotFrequencySpectrum(x,y,1,0,1); % % EXAMPLE 2 % load testsignalA.mat; % subplot(211); % plot(x,y); % subplot(212); % PlotFrequencySpectrum(x,y,2,0,1); sizex=size(xx);if sizex(1)3;smoothtype=3;end if smoothtype<1;smoothtype=1;end if smoothwidth<1;smoothwidth=1;end smoothwidth=round(smoothwidth); peakgroup=round(peakgroup); if smoothwidth>1, d=fastsmooth(deriv(y),smoothwidth,smoothtype); else d=deriv(y); end n=round(peakgroup/2+1); P=[0 0 0]; vectorlength=length(y); peak=1; AmpTest=AmpThreshold; PeakX=0; PeakY=0; % plot(x,d,'.') for j=2*round(smoothwidth/2)-1:length(y)-smoothwidth-1, if sign(d(j)) > sign (d(j+1)), % Detects zero-crossing if d(j)-d(j+1) > SlopeThreshold, % if slope of derivative is larger than SlopeThreshold xx=zeros(size(peakgroup));yy=zeros(size(peakgroup)); for k=1:peakgroup, % Create sub-group of points near peak groupindex=j+k-n+2; if groupindex<1, groupindex=1;end if groupindex>vectorlength, groupindex=vectorlength;end xx(k)=x(groupindex);yy(k)=y(groupindex); end if peakgroup<3; PeakY=max(yy); else PeakY=mean(yy); end pindex=val2ind(yy,PeakY); PeakX=xx(pindex(1)); % Construct matrix P. One row for each peak detected, % containing the peak number, peak position (x-value) and % peak height (y-value). If peak measurement fails and % results in NaN, or if the measured peak height is less % than AmpThreshold, skip this peak if isnan(PeakX) || isnan(PeakY) || PeakY