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

main.m
% Create a half-wave rectified sine wave
t = 0:0.001:1; % time vector from 0 to 1 with step size 0.001
f = 50; % frequency of the sine wave
x = sin(2*pi*f*t); % create a sine wave
x(x<0) = 0; % half-wave rectification

% Plot the half-wave rectified sine wave in time domain
figure;
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 domain
N = length(t);
Fs = 1/(t(2)-t(1)); % Sampling frequency
X = abs(fft(x))/N; % Compute the Fourier Transform and normalize
frequencies = linspace(0, Fs, N);
subplot(2,1,2);
stem(frequencies(1:N/2), 2*X(1:N/2)) % Plot only the positive frequencies
title('Half-wave rectified sine wave in frequency domain');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
821 chars
25 lines

related categories

gistlibby LogSnag