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

main.m
% Define the parameters
A = 1;  % Amplitude of the sine wave
f = 1;  % Frequency of the sine wave
Fs = 1000;  % Sampling frequency
t = 0:1/Fs:1;  % Time vector
rectified_sine = A * abs(sin(2*pi*f*t));  % Rectified half-wave sine signal

% Compute the Fourier Transform
N = length(rectified_sine);  % Length of the signal
Y = fft(rectified_sine)/N;  % Normalize the FFT results
frequencies = 0:Fs/N:Fs*(N-1)/N;  % Frequency vector

% Extract the first seven Fourier coefficients
nCoefficients = 7;
coefficients = Y(1:nCoefficients+1);

% Calculate frequencies and amplitudes of the coefficients
frequencies_coeff = (0:nCoefficients)*(Fs/N);  % Frequencies of the coefficients
amplitudes_coeff = abs(coefficients);  % Amplitudes of the coefficients

% Display the frequencies and amplitudes
disp('Frequencies of the first seven Fourier coefficients:');
disp(frequencies_coeff);
disp('Amplitudes of the first seven Fourier coefficients:');
disp(amplitudes_coeff);
961 chars
26 lines

This code calculates the frequencies and amplitudes of the first seven Fourier coefficients of a rectified half-wave sine in MATLAB.

gistlibby LogSnag