compute the frequencies and the amplitudes of the first seven fourier coefficients of a rectified half-wave 50hz sine 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

% Create the rectified half-wave sine wave
x = abs(sin(2*pi*f*t)); 

% Perform Fourier transform
X = fft(x);

% Calculate the frequencies
frequencies = (0:length(X)-1)*fs/length(X);

% Compute the amplitudes of the first seven Fourier coefficients
amplitudes = abs(X(1:8));

% Display the frequencies and amplitudes
disp('Frequencies:');
disp(frequencies(1:8));
disp('Amplitudes:');
disp(amplitudes);
523 chars
23 lines

In this code, we first define the parameters such as the frequency of the sine wave (50 Hz), the sampling frequency (fs), and create the time vector. We then generate the rectified half-wave sine wave, perform the Fourier transform on it, calculate the frequencies of the Fourier coefficients, and compute the amplitudes of the first seven coefficients. Finally, we display the frequencies and amplitudes.

gistlibby LogSnag