plot the first seven fourier coefficients versus frequency of a rectified 50hz sine wave in matlab

main.m
% Define the parameters
f = 50; % frequency of the sine wave
fs = 1000; % sampling frequency
t = 0:1/fs:1; % time vector
x = abs(sin(2*pi*f*t)); % rectified 50 Hz sine wave

% Compute the Fourier Transform
N = length(x); % length of the signal
X = fft(x)/N; % compute the Fourier Transform and normalize

% Frequencies vector
frequencies = linspace(0, fs, N);

% Plot the first seven Fourier coefficients versus frequency
figure;
stem(frequencies(1:7), abs(X(1:7)));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('First seven Fourier coefficients of a rectified 50Hz sine wave');
587 chars
20 lines

This code will plot the first seven Fourier coefficients versus frequency for a rectified 50Hz sine wave sampled at 1000Hz in MATLAB.

gistlibby LogSnag