create plots of an half-wave rectified sine both in time and in frequency using the first seven fourier coefficients only in matlab
main.m
% Time domain plot of half-wave rectified sine wavef = 1; % Frequency of the sine wavet = 0:0.001:2; % Time rangex = abs(sin(2*pi*f*t)); % Half-wave rectified sine wavefigure;
plot(t, x);
title('Half-wave rectified sine wave in Time domain');
xlabel('Time');
ylabel('Amplitude');
% Fourier series representation of half-wave rectified sine waven = 1:7; % First seven Fourier coefficientsa = 4/pi * (1./n); % Compute Fourier coefficientsN = 500; % Number of samples for frequency domain plotfs = N/(t(end)-t(1)); % Sampling frequencyfrequencies = (-N/2:N/2-1)*fs/N; % Frequency axisX = zeros(size(frequencies));
fori = 1:length(a)
X = X + a(i) * sinc(frequencies*i); % Amplitude spectrum using Fourier coefficientsendfigure;
plot(frequencies, abs(X));
title('Half-wave rectified sine wave in Frequency domain');
xlabel('Frequency');
ylabel('Magnitude');