compute frequencies and amplitudes of the first seven fourier coefficients of a rectified half-wave 50hz sine in matlab

main.m
% Define the parameters
f = 50; % frequency in Hz
t = 0:0.0001:0.1; % time vector from 0 to 0.1 seconds
x = sin(2*pi*f*t); % generate a sine wave at 50 Hz

% Rectify the sine wave
rectified_x = x;
rectified_x(rectified_x < 0) = 0;

% Compute the Fourier Series Coefficients
N = 7; % number of coefficients
A = zeros(N, 1);
B = zeros(N, 1);

for n = 1:N
    A(n) = 2/((n)*pi) * sum(rectified_x.*sin(2*pi*(n)*f*t));
    B(n) = 2/((n)*pi) * sum(rectified_x.*cos(2*pi*(n)*f*t));
end

% Compute frequencies and amplitudes
frequencies = n*f; % frequency of each coefficient
amplitudes = sqrt(A.^2 + B.^2); % amplitude of each coefficient

disp([frequencies' amplitudes'])
666 chars
25 lines

This MATLAB code snippet computes the frequencies and amplitudes of the first seven Fourier coefficients of a rectified half-wave 50 Hz sine wave. The code first generates a 50 Hz sine wave, rectifies it to obtain the half-wave rectified sine wave, and then computes the Fourier coefficients using a loop. Finally, it calculates the frequencies and amplitudes of the Fourier coefficients and displays them in the command window.

gistlibby LogSnag