create graph of a half-wave rectified sine in time and in frequency in matlab
main.m
% Create a half-wave rectified sine wavet = 0:0.001:1; % time vector from 0 to 1 with step size 0.001f = 50; % frequency of the sine wavex = sin(2*pi*f*t); % create a sine wavex(x<0) = 0; % half-wave rectification% Plot the half-wave rectified sine wave in time domainfigure;
subplot(2,1,1);
plot(t, x);
title('Half-wave rectified sine wave in time domain');
xlabel('Time (s)');
ylabel('Amplitude');
% Plot the half-wave rectified sine wave in frequency domainN = length(t);
Fs = 1/(t(2)-t(1)); % Sampling frequencyX = abs(fft(x))/N; % Compute the Fourier Transform and normalizefrequencies = linspace(0, Fs, N);
subplot(2,1,2);
stem(frequencies(1:N/2), 2*X(1:N/2)) % Plot only the positive frequenciestitle('Half-wave rectified sine wave in frequency domain');
xlabel('Frequency (Hz)');
ylabel('Magnitude');