What is wrong with my Code? (Fast Fourier Transform)
5 views (last 30 days)
Show older comments
Hi, there is a signal that I was trying to port into matlab just for a practise to teach myself about FFt.
g(t) = rect(t/T) cos(2*pi*fc*t) where fc is the carrier frequency.
T is the duration of the pulse.
1-How can I determine the time interval correctly with using Fourier Transform Table?
2-How can I plot the amplitude spectrum of the signal?
It says the spectrum of g (t) is calculated, via Fourier Transform table, as G (f) =Tsinc [T (f −f c)].
You can take T = 0.002 and fc = 6000 or whatever you pick.
ı just want to understand when we multiply by rectangular pulse with a high frequency cos signal
Here is my code that I tried but no luck:
%A1.2
clc;
clear;
close all;
t = -0.01 : 0.002 : 0.01; %------------> I am not sure those time intervals.....
fc = 6000;
g = rect(t/T).*cos(2*pi*fc*t); %Signal itself
subplot(2,1,1);
plot(t, g);
xlabel('t');
ylabel('x(t)');
title('Signal');
Fshift_x = fftshift(fft(g)); %calculating shifted frequency spectrum
n = length(Fshift_x); %length of interval
A = abs(Fshift_x)/n; %amplitude spectrum
f = linspace(-T/2, T/2, n); %frequency values
subplot(2,1,2);
plot(f, A);
xlabel('f');
ylabel('A(f)');
title('Amplitude Spectrum');
3 Comments
Accepted Answer
Star Strider
on 9 Mar 2022
%A1.2
T = 0.002;
t = -0.01 : 0.0002 : 0.01; %------------> I am not sure those time intervals.....
fc = 6000;
g = rectpuls(t/T).*cos(2*pi*fc*t); %Signal itself
subplot(2,1,1);
plot(t, g);
xlabel('t');
ylabel('x(t)');
title('Signal');
grid
Fshift_x = fftshift(fft(g)); %calculating shifted frequency spectrum
n = length(Fshift_x); %length of interval
A = (Fshift_x)/n; %amplitude spectrum
f = linspace(-T/2, T/2, n); %frequency values
subplot(2,1,2);
plot(f, real(A));
hold on
plot(f, imag(A));
plot(f, abs(A));
hold off
xlabel('f');
ylabel('A(f)');
title('Amplitude Spectrum');
grid
legend('Re(A)','Imag(A)','|A|','location','best')
Change the step interval in ‘t’ to get different results.
.
4 Comments
More Answers (1)
Paul
on 10 Mar 2022
The problem can be solved symbolically or numerically.
First symbolically.
% define duration and frequency
Td = 0.002;
fc = 6000; % Hz
syms t w f real
r(t) = rectangularPulse(-0.5,0.5,t);
g(t) = r(t/Td)*cos(2*sym(pi)*fc*t);
% plot the signal
figure
fplot(g(t),[-0.004 0.004]) % as expected, 12 cycles over 0.002 seconds
% its Fourier transform
G(w) = simplify(fourier(g(t),t,w));
% convert to Hz
G(f) = G(2*sym(pi)*f) % G(f) is real
% plot abs(G(f))
figure
fplot(abs(G(f)),[-12000 12000]); ylim([0 1e-3])
xlabel('Hz')
Now numerically.
% need to pick a sampling frequency such that Fs/fc is an integer greater
% than 2
Fs = fc*10;
N = Fs*Td;
% samples over the finite duration of the sequence
tval = -0.001 + (0:N-1)/Fs;
gval = rectpuls(tval/Td).*cos(2*pi*fc*tval);
% compare samples to signal
figure
fplot(g(t),[-0.004 0.004])
hold on
plot(tval,gval,'o')
% DFT of gval.
% Because only interested in amplitude spectrum, don't worry that first point of gval corresponds to t = -0.001 instead of t = 0.
% Need to zero pad because the underlying signal is finite duration and
% the samples are just of the cosine.
% Need to scale the dft by 1/Fs to match the continuous time Fourier
% transform.
nfft = 1024;
gdft = fft(gval,nfft)/Fs;
wdft = (0:nfft-1)/nfft*Fs;
% compare to G(f) for f > 0. Could use fftshift for the negative
% frequencies if desired
figure
fplot(abs(G(f)),[0 12000]);
xlabel('Hz')
hold on
plot(wdft(wdft < Fs/2),abs(gdft(wdft < Fs/2)),'o')
axis([0 12000 0 0.001])
2 Comments
Paul
on 11 Mar 2022
You're very welcome. Feel free to post back if you have any questions on the explanation.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!