How to create fft Magnitude and Phase using stem Matlab?

15 次查看(过去 30 天)
With the function
cos(w0t)
, first create your own time domain data and second, conduct
the FT (Fourier transform). When you solve the problem, use the MATLAB codes with
two “for ~ end” loops without using already built-in functions except for the fundamental
ones such as “linspace” and “cos”. Then draw the result of the FT in frequency domain in
terms of “Magnitude vs. freq.” and “Phase vs. freq.” by using “stem” instead of “plot”.
Here, let’s set w0=2pi/T and T = 1(sec).

回答(1 个)

EE_student
EE_student 2021-6-19
编辑:EE_student 2021-6-19
This is the amplitude spectrum for cos(2*pi*1*t) for a 2 second long signal length. I will leave the phase spectrum up to you.
clear
clc
clearAllMemoizedCaches
clear
sampleR= 1000; % sample rate in Hz
t= 0:1/sampleR:2; % time vector
N= length(t); % number of samples
signal= cos(2*pi*1*t); % defining signal
% random dc offset,amplitude and frequency
t_hat= (0:N-1)/N ; % normalised time vector
harmonics= zeros(size(signal)); % empty vector that stores the computed Harmonics
plot(t,signal)
grid on
title('Signal(t)')
xlabel('Time')
ylabel('Amplitude')
for delta_f=1:N
e= exp(-1j*2*pi*(delta_f-1)*t_hat);
harmonics(delta_f)= (sum(signal.*e))/N;
end
freq= linspace(0,sampleR/2,floor(N/2)+1); % scaling frequency
amp= abs(harmonics); % extracting amplitudes and scaling them
amp(2:length(freq)) = 2*amp(2:length(freq)); % double only the ac harmonics
stem(freq,amp(1:length(freq))) % making the vectos same in length
xlim([0 10]) % limits must be included
title('DFT of Signal(t)')
xlabel('Freq/Hz')
ylabel('Amplitude')
grid on

类别

Help CenterFile Exchange 中查找有关 Spectral Measurements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by