What is wrong with my Code? (Fast Fourier Transform)

5 次查看(过去 30 天)
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
Unrecognized function or variable 'T'.
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 个评论
cikalekli
cikalekli 2022-3-9
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
cikalekli
cikalekli 2022-3-9
Alsoi I am sorry that if I made my question looks bizarre.
I hope everything is clear.
If it's not, then please ask me to clarify.

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2022-3-9
The ‘rect’ funciton does not exist in MATLAB, so I use rectpuls here instead —
%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 个评论
cikalekli
cikalekli 2022-3-10
Again thank you so muchi I'll try what you have said at the end of your sentence today

请先登录,再进行评论。

更多回答(1 个)

Paul
Paul 2022-3-10
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
G(f) = 
% 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 个评论
cikalekli
cikalekli 2022-3-11
编辑:cikalekli 2022-3-11
Ah that's a impeccable explanation right here! Thank you so much for also putting explanatory comments in your each code lines. I've just learned a new pathway about Fourier Transform coding thanks to you. ^_^
Paul
Paul 2022-3-11
You're very welcome. Feel free to post back if you have any questions on the explanation.

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by