create graph of a half-wave rectified sine in time and in frequency without using fft in matlab

You can create a graph of a half-wave rectified sine wave in the time domain using simple mathematical operations in MATLAB. To plot the waveform in the frequency domain without using the Fast Fourier Transform (FFT), you can perform a Fourier series analysis.

Here is some sample MATLAB code to generate the plots:

main.m
% Define the parameters
f = 1;                  % Frequency of the sine wave (Hz)
A = 1;                  % Amplitude of the sine wave
t = 0:0.001:1;          % Time vector from 0 to 1 second with a step size of 0.001 seconds

% Create the half-wave rectified sine wave in the time domain
x = A*sin(2*pi*f*t);
rectified_x = max(x, 0);

% Plot the half-wave rectified sine wave in the time domain
figure;
subplot(2, 1, 1);
plot(t, x, 'b', 'LineWidth', 1.5);
title('Half-Wave Rectified Sine Wave in Time Domain');
xlabel('Time (s)');
ylabel('Amplitude');

subplot(2, 1, 2);
plot(t, rectified_x, 'r', 'LineWidth', 1.5);
title('Half-Wave Rectified Sine Wave in Time Domain after Rectification');
xlabel('Time (s)');
ylabel('Amplitude');

% Perform Fourier series analysis to obtain the frequency domain representation
N = 10;                 % Number of harmonics to consider
frequencies = 0:N;      % Frequencies to analyze

% Calculate the Fourier series coefficients
an = zeros(size(frequencies));
bn = (2*A/pi)./(1:2:2*N)*(-1).^(floor((1:2:2*N)/2));
bn(2:2:end) = 0;  % Odd harmonics only as we are dealing with a half-wave rectified signal

% Reconstruct the signal in frequency domain using computed coefficients
frequency_x = A/2;  % DC component (fundamental frequency)
for k = 1:N
    frequency_x = frequency_x + an(k)*cos(2*pi*k*f*t) + bn(k)*sin(2*pi*k*f*t);
end

% Plot the half-wave rectified sine wave in the frequency domain
figure;
plot(t, rectified_x, 'r', 'LineWidth', 1.5);
title('Half-Wave Rectified Sine Wave in Frequency Domain (Fourier Series Approximation)');
xlabel('Time (s)');
ylabel('Amplitude');
1620 chars
45 lines

This code will generate plots of the half-wave rectified sine wave in both the time and frequency domains using Fourier series analysis in MATLAB.

related categories

gistlibby LogSnag