MATLAB: How can i find the peaks of the signal and get a phase and magnitude plot

magnitude spectrum

Best Answer

The easiest way is to create and plot a one-sided fft (link). The phase plot uses the angle function.
To find the peaks and their locations, use the Signal Processing Toolbox findpeaks (link) function.
Example ā€”
N = 1000;
t = linspace(0, 10*pi, N); % Time Vector
Fs = 1/(t(2)-t(1)); % Sampling Frequency
Fn = Fs/2; % Nyquist Frequency
s = sin(2*pi*5*t); % Signal Vector
FTs = fft(s)/N; % Divide By ā€˜Nā€™ To Normalise Amplitudes
Fv = linspace(0, 1, fix(N/2)+1)*Fn; % Frequency Vector (1-Sided)
Iv = 1:length(Fv); % Index Vector
figure(1)
subplot(2,1,1)
plot(Fv, abs(FTs(Iv))*2)
grid
ylabel('Amplitude')
title('Amplitude Plot')
subplot(2,1,2)
plot(Fv, angle(FTs(Iv))*180/pi)
grid
xlabel('Frequency (Hz)')
ylabel('Phase (Ā°)')
title('Phase Plot')