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 vector
t = 0:0.001:1;

% Define the frequency of the sine wave
f = 1;

% Generate the half-rectified sine wave in time domain
x_time = abs(sin(2*pi*f*t));

% Fourier series with first seven coefficients
N = 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 coefficients
x_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 signals
subplot(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');
831 chars
40 lines

related categories

gistlibby LogSnag