create half-rectified sine plot in time and create half-wave sine plot in frequency using the first seven fourier coefficients without fft in matlab
main.m
% Define time vectort = 0:0.001:1;
% Define the frequency of the sine wavef = 1;
% Generate the half-rectified sine wave in time domainx_time = abs(sin(2*pi*f*t));
% Fourier series with first seven coefficientsN = 7;
a_0 = 0;
a_n = zeros(1,N);
b_n = zeros(1,N);
for n = 1:N
a_n(n) = 2/(n*pi)*(1-(-1)^n);
b_n(n) = 2/(n*pi)*(1-(-1)^n)*(-1)^n;
end% Generate half-wave sine plot in frequency domain with first seven coefficientsx_freq = a_0/2;
for n = 1:N
x_freq = x_freq + a_n(n)*cos(2*pi*n*f*t) + b_n(n)*sin(2*pi*n*f*t);
end% Plot the time domain and frequency domain signalssubplot(2,1,1);
plot(t, x_time);
xlabel('Time');
ylabel('Amplitude');
title('Half-Rectified Sine Wave');
subplot(2,1,2);
plot(t, x_freq);
xlabel('Frequency');
ylabel('Amplitude');
title('Half-Wave Sine Wave in Frequency Domain');