plot an half-wave rectified 50hz sine in time and plot first seven harmonics in frequency in matlab

main.m
% Generate a half-wave rectified sine wave
f = 50;  % Frequency of the sine wave
t = 0:0.0001:1;  % Time vector from 0 to 1 second
x = sin(2*pi*f*t);  % Generate the sine wave
x(x<0) = 0;  % Half-wave rectification

% Plot the half-wave rectified sine wave in time domain
figure;
plot(t, x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Half-wave rectified 50Hz sine wave in time domain');

% Plot the first seven harmonics in frequency domain
N = 7;  % Number of harmonics
Fs = 1000;  % Sampling frequency
Y = fft(x, Fs);  % Compute Fast Fourier Transform
frequencies = linspace(0, Fs, length(Y));

figure;
stem(frequencies(1:Fs/2), abs(Y(1:Fs/2)));
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('First seven harmonics of the half-wave rectified sine wave');
770 chars
25 lines

gistlibby LogSnag