Clear Filters
Clear Filters

Hello, i studied this fft code in mathworks but i have some challenge trying to use it. please, you can be of help. below is the code and my challenge

2 views (last 30 days)
%Use Fourier transforms to find the frequency components of a signal buried in noise.
% Specify the parameters of a signal with a sampling frequency of 1 kHz and a signal duration of 1 second.
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sampling period
L = 1500; % Length of signal
t = (0:L-1)*T; % Time vector
%Form a signal containing a 50 Hz sinusoid of amplitude 0.7 and a 120 Hz sinusoid of amplitude 1.
S = 0.7*sin(2*pi*50*t) + sin(2*pi*120*t);
%Corrupt the signal with zero-mean white noise with a variance of 4.
X = S + 2*randn(size(t));
%Plot the noisy signal in the time domain. It is difficult to identify the frequency components by looking at the signal X(t).
plot(1000*t(1:50),X(1:50))
title('Signal Corrupted with Zero-Mean Random Noise')
xlabel('t (milliseconds)')
ylabel('X(t)')
%Compute the Fourier transform of the signal.
Y = fft(X);
%Compute the two-sided spectrum P2. Then compute the single-sided spectrum
%P1 based on P2 and the even-valued signal length L.
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
%Define the frequency domain f and plot the single-sided amplitude spectrum P1.
%The amplitudes are not exactly at 0.7 and 1, as expected, because of the added noise.
%On average, longer signals produce better frequency approximations.
f = Fs*(0:(L/2))/L;
plot(f,P1)
title('Single-Sided Amplitude Spectrum of X(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
%Now, take the Fourier transform of the original, uncorrupted signal and retrieve the exact amplitudes, 0.7 and 1.0.
Y = fft(S);
P2 = abs(Y/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
plot(f,P1)
title('Single-Sided Amplitude Spectrum of S(t)')
xlabel('f (Hz)')
ylabel('|P1(f)|')
MY CHALLENGE IN THE CODE
Fs = 1350; % Sampling frequency
T = 1/Fs; % Sampling period
L = 5000; % Length of signal
t = (0:L-1)*T; % Time vector
S50 = Iabc(:,2); %signal containing 50Hz sinusoid
X = Ia(:,2); %sampled signal containing a 1350Hz sinusoid
%Plot the sampled signal in the time domain
figure, plot(1350*t(1:100),X(1:100))
title('sampled Signal at 1350Hz')
xlabel('t (milliseconds)')
ylabel('X(t)')
% compute the fourier transform
Y = fft(X); %sampled signal
P2 = abs(Y/L); %%Why (Y/L)?
P1 = P2(1:L/2+1); %%Why and how come (1:L/2+1))?
i recieved an error message saying...
Index exceeds matrix dimensions."Error using plot.
Vectors must be the same lengths.
Error in FFT_trail (line 18)
P1(2:end-1) = 2*P1(2:end-1); %%how come P1(2:end-1) = 2*P1(2:end-1)?
f = Fs*(0:(L/2))/L; %%how come?
figure, plot(f,P1)
  1 Comment
Image Analyst
Image Analyst on 14 Jul 2017
Can you give us code that reproduces the error, not an error like this:
Undefined function or variable 'Iabc'.
Error in test2 (line 56)
S50 = Iabc(:,2); %signal containing 50Hz sinusoid
You need to provide Iabc so I can run the code.

Sign in to comment.

Answers (0)

Categories

Find more on Fourier Analysis and Filtering in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!