create graph of a half-wave rectified sine in time and in frequency using the first seven fourier coefficients in matlab
main.m
% Define the frequencies and coefficients for the Fourier seriesfrequencies = [01234567]; %assuming the first 7 Fourier coefficientscoefficients = [02/(pi*1) 02/(3*pi) 02/(5*pi) 02/(7*pi)];
% Define the fundamental parametersfrequency = 1; % frequency of the sine waveamplitude = max(coefficients); % amplitude of the sine wave% Create the time vectort = 0:0.001:2/frequency; % time from 0 to 2 periods% Create the half-wave rectified sine waverectified_sine = zeros(size(t));
for n = 1:length(frequencies)
rectified_sine = rectified_sine + coefficients(n)*sin(2*pi*frequencies(n)*frequency*t);
endrectified_sine(rectified_sine < 0) = 0; % half-wave rectification% Plot the time-domain signalsubplot(2,1,1);
plot(t, rectified_sine);
xlabel('Time');
ylabel('Amplitude');
title('Half-Wave Rectified Sine Wave (Time Domain)');
% Perform Fourier TransformL = length(t); % Length of signalNFFT = 2^nextpow2(L); % Next power of 2 from length of yfft_rectified_sine = fft(rectified_sine,NFFT)/L;
f = 1/(2*0.001)*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.subplot(2,1,2);
plot(f,2*abs(fft_rectified_sine(1:NFFT/2+1)));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Half-Wave Rectified Sine Wave (Frequency Domain)');