I am trying to find peaks in spectrum but I need to extract only peak of base frequency and its harmonics, red rectangular. How to exclude anything before base frequency and only include base frequency and its 3 harmonics. I use this code but it does not help. Any idea?
pks = findpeaks(q);
findpeaks(q,'MinPeakDistance',99)
%findpeaks(q,'MinPeakHeight',0.0004)
xlim([0.1 500])
When using:
Fs = 1000;
t = 0:0.001:1-0.001; % 250-Hz sine wave modulated at 100 Hz
x = [1 cos(2*pi*100*t)].*cos(2*pi*250*t);
%envspectrum(x,Fs)
[ES,F]=envspectrum(x,Fs);
%%
findpeaks(ES,F)
% Now for only > 99 Hz (choose the freq you fancy)
idx = F >= 99; % greater than 99 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 99
% Good? Keep the values of amplitud and location (in frequencies) of the ES
[pks,loc] = findpeaks(ES(idx),F(idx));
% If you just want to have the first 5 peaks (or the n? you choose):
% Select only 3 first.
if length(pks) > 3 % Check you didnt get less peaks
pks = pks(1:3);
loc = loc(1:3);
end
% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')
I get this:
CodePudding user response:
EDITED to adapt to new code with an example. We are applying on the envelope spectrum of a signal - [ES,F] = envspectrum(sig,Fs);
-, so we know the signal and its frequency sampling (fs).
Still being the same process. You can just compute the values in findpeaks()
for only those samples of your signal which are higher than 250 Hz. For that purpose, you define a logical array for the frequencies values you want to:
idx = F >= 250; % 250 Hz
And apply this logical index to the envelop spectrum of your signal and frequencies where you want to apply the function findpeaks()
:
[pks,loc] = findpeaks(ES(idx),F(idx));
Example:
% Let's create a signal with fs = 2500 Hz
% This is just to create an example, don't worry about these lines
fs = 2500;
f0 = 25;
n = 8;
d = 0.02;
p = 0.12;
t = 0:1/fs:1-1/fs;
z = [1 0.5 0.2 0.1 0.05]*sin(2*pi*f0*[1 2 3 4 5]'.*t)/5;
% z: our signal
% t: time array of the signal (to plot)
% How does the signal look?
plot(t,z)
% Calculating the envelope signal and its spectrum
[ES,F]=envspectrum(z,fs);
% Plot the envelop if you want:
envspectrum(z,fs)
% ES: Envelope spectrum of the signal
% F: array of frequencies used for the spectrum (half our fs for Nyquist Theorem)
% Find the peaks of the envelope spectrum
% Let's see how it is for all the envelope
findpeaks(ES,F)
% Now for only > 250 Hz (choose the freq you fancy)
idx = F >= 250; % greater than 250 Hz
findpeaks(ES(idx),F(idx)) % idx only select those F > 250
% Good? Keep the values of amplitud and location (in frequencies) of the ES
% Use 'NPeaks' input to tell function to select only 5 first peaks it can find.
[pks,loc] = findpeaks(ES(idx),F(idx), 'NPeaks', 5);
% To plot the peaks in the envelope
plot(F(idx),ES(idx),loc,pks,'r*')
% Note in this example the first peak (in 250 Hz) is not selected
% because is not a local maximum when we narrow the envelope.
% If the function select false peaks somehow, you can use
%'MinPeakHeight' to specify an absolute amplitude, 'MinPeakProminence '
%for a relative amplitude or 'Threshold' inputs for a better fit.
https://uk.mathworks.com/matlabcentral/answers/768537-use-findpeaks-on-specific-frequency-range
https://uk.mathworks.com/help/signal/ref/findpeaks.html#responsive_offcanvas