Hi. I want to plot frequency spectrum of a signal. I got this coding based on the sources that I found from the internet but my lecturer said this is not frequency spectrum. Can somebody help me on this? close all; %Define number of samples to take fs = 8000; f = 400; %Hz %Define signal t = 0:1/fs:1-1/fs; signal = sin(2*pi*f*t); %Plot to illustrate that it is a sine wave plot(t, signal); title('Time-Domain signal'); %Take fourier transform fftSignal = fft(signal); %apply fftshift to put it in the form we are used to (see documentation) fftSignal = fftshift(fftSignal); %Next, calculate the frequency axis, which is defined by the sampling rate f = fs/2*linspace(-1,1,fs); %Since the signal is complex, we need to plot the magnitude to get it to %look right, so we use abs (absolute value) figure; plot(f, abs(fftSignal)); title('magnitude FFT of sine'); xlabel('Frequency (Hz)'); ylabel('magnitude'); %noise noise = 2*randn(size(signal)); figure, plot(t,noise), title('Time-Domain Noise'); fftNoise = fft(noise); fftNoise = fftshift(fftNoise); figure, plot(f,abs(fftNoise)), title('Magnitude FFT of noise'); xlabel('Frequency (Hz)'); ylabel('magnitude'); %noisy signal noisySignal = signal + noise; figure, plot(t,noisySignal), title('Time-Domain Noisy Signal'); fftNoisySignal = fft(noisySignal); fftNoisySignal = fftshift(fftNoisySignal); figure, plot(f,abs(fftNoisySignal)), title('Magnitude FFT of noisy signal'); xlabel('Frequency (Hz)'); ylabel('magnitude');
Neeta Dsouza answered .
2025-11-20
Your code is close to generating a frequency spectrum, but it needs adjustments to ensure it correctly computes and visualizes the frequency content of your signal. Here's a step-by-step guide to correct and refine your code:
Frequency Axis Definition:
f = fs/2*linspace(-1,1,fs);) is incorrect. The number of points in fftSignal determines the resolution of the frequency axis.FFT Normalization:
Positive Frequencies:
Clear Titles and Labels:
close all;
clear;
% Define parameters
fs = 8000; % Sampling frequency in Hz
f = 400; % Signal frequency in Hz
t = 0:1/fs:1-1/fs; % Time vector (1 second duration)
% Define signal
signal = sin(2*pi*f*t);
% Plot time-domain signal
figure;
plot(t, signal);
title('Time-Domain Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Compute FFT
N = length(signal); % Number of samples
fftSignal = fft(signal); % Perform FFT
fftSignal = fftSignal / N; % Normalize FFT output
% Compute frequency axis
f = (0:N-1)*(fs/N); % Frequency vector (0 to fs)
halfRange = 1:floor(N/2); % Consider only the positive frequencies
% Plot frequency spectrum
figure;
plot(f(halfRange), abs(fftSignal(halfRange))); % Magnitude spectrum
title('Frequency Spectrum of Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
% Add noise
noise = 2*randn(size(signal)); % Generate random noise
noisySignal = signal + noise; % Add noise to the signal
% Plot time-domain noisy signal
figure;
plot(t, noisySignal);
title('Time-Domain Noisy Signal');
xlabel('Time (s)');
ylabel('Amplitude');
% Compute FFT of noisy signal
fftNoisySignal = fft(noisySignal);
fftNoisySignal = fftNoisySignal / N;
% Plot frequency spectrum of noisy signal
figure;
plot(f(halfRange), abs(fftNoisySignal(halfRange))); % Magnitude spectrum
title('Frequency Spectrum of Noisy Signal');
xlabel('Frequency (Hz)');
ylabel('Magnitude');