create graph of a half-wave rectified sine in time and in frequency using the first seven fourier coefficients without using fft in matlab

To create a graph of a half-wave rectified sine wave in time and in frequency using the first seven Fourier coefficients without using FFT in MATLAB, you can directly calculate and plot the time and frequency domain representations.

Here is some sample code to achieve this:

main.m
% Parameters
Fs = 1000;        % Sampling frequency
T = 1/Fs;         % Sampling period
L = 1000;         % Length of signal
t = (0:L-1)*T;    % Time vector
f = Fs*(0:(L/2))/L; % Frequency vector

% Fourier coefficients
A0 = 2/pi;
A = zeros(1, 7);
B = zeros(1, 7);
for n = 1:7
    A(n) = -2/(pi*(4*n^2-1));
end

% Create the half-wave rectified sine wave
x = A0/2;
for n = 1:7
    x = x + A(n)*sin(2*pi*(2*n-1)*50*t);
end

% Plot the time domain signal
subplot(2,1,1);
plot(t, x);
title('Half-Wave Rectified Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');

% Calculate the frequency components
X = zeros(1, length(f));
for m = 1:length(f)
    for n = 1:7
        X(m) = X(m) + A(n)*sin(2*pi*(2*n-1)*50*T*f(m));
    end
end

% Plot the frequency spectrum
subplot(2,1,2);
plot(f, abs(X));
title('Frequency Spectrum of Half-Wave Rectified Sine Wave');
xlabel('Frequency (Hz)');
ylabel('Magnitude');

903 chars
44 lines

This code snippet generates a half-wave rectified sine wave signal using the first seven Fourier coefficients and then plots it in both time and frequency domains.

Ensure you adapt the code to your specific requirements and signal parameters.

gistlibby LogSnag