Getting an FFT signal into a for loop.

6 views (last 30 days)
%% Q1
clc;
clear;
n=0:1000;
fs=8000; % Sampling frequency, Hz
nfft=1024; % Number of samples
nf=fs*(-nfft/2:nfft/2-1)./nfft; % establishing frequency points (x-axis), figure 2
time=(1:length(n))./fs; % establishing time points (x-axis), figure 1
x1=5*cos(2*pi*500*time);
x2=5*cos(2*pi*1200*time+0.25*pi);
x3=5*cos(2*pi*1800*time+0.5*pi);
x=x1+x2+x3; % Sum of three signals
plot(time,x)
xlabel('Time (s)') % x-axis label
ylabel('Amplitude') % y-axis label
title('Sum of Sampled Signals') % title
figure;
X=fftshift(fft(x,nfft)); % FFT of sum of 3 signals
plot(nf,abs(X))
xlabel('frequency(Hz)') % x-axis label
ylabel('Magnitude') % y-axis label
title('Frequency Response') % title
I need help getting this last section into a for loop, i can not figure it out and need help. The graph labeled "frequency response" is the correct graph I need however i need the means of getting that graph to be from a for loop.

Accepted Answer

VBBV
VBBV on 27 Oct 2021
clc;
clear;
n=0:1000;
fs=8000; % Sampling frequency, Hz
nfft=1024; % Number of samples
nf=fs*(-nfft/2:nfft/2-1)./nfft; % establishing frequency points (x-axis), figure 2
time=(1:length(n))./fs ;% establishing time points (x-axis), figure 1
% x1=5*cos(2*pi*500*time);
% x2=5*cos(2*pi*1200*time+0.25*pi);
% x3=5*cos(2*pi*1800*time+0.5*pi);
% x=x1+x2+x3; % Sum of three signals
%plot(time,x)
% xlabel('Time (s)') % x-axis label
% ylabel('Amplitude') % y-axis label
% title('Sum of Sampled Signals') % title
for i = 1:length(n)
%figure;
x1(i)=5*cos(2*pi*500*time(i));
x2(i)=5*cos(2*pi*1200*time(i)+0.25*pi);
x3(i)=5*cos(2*pi*1800*time(i)+0.5*pi);
x(i)=x1(i)+x2(i)+x3(i); % Sum of three signals
X=fftshift(fft(x(1:i),nfft)); % FFT of sum of 3 signals
plot(nf,abs(X))
end
xlabel('frequency(Hz)') % x-axis label
ylabel('Magnitude') % y-axis label
title('Frequency Response') % title
  1 Comment
Connor Roche
Connor Roche on 27 Oct 2021
is there any way to not use "fft" or "fftshift" in this code?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!