create a half rectified sine wave in time and in frequency in matlab

To create a half rectified sine wave in time and frequency domain in Matlab, you can use the following code:

main.m
% Parameters
f = 1; % Frequency of the sine wave
t = 0:0.001:1; % Time vector
A = 1; % Amplitude

% Generate the half rectified sine wave in time domain
x = A * abs(sin(2*pi*f*t));

% Compute the Fourier transform
X = fft(x);

% Compute the one-sided spectrum
L = length(x);
P2 = abs(X/L);
P1 = P2(1:L/2+1);
P1(2:end-1) = 2*P1(2:end-1);
frequencies = 1/0.001 * (0:(L/2))/L;

% Plot the time domain wave
subplot(2,1,1)
plot(t, x)
title('Half Rectified Sine Wave in Time Domain')
xlabel('Time')
ylabel('Amplitude')

% Plot the frequency spectrum
subplot(2,1,2)
plot(frequencies, P1)
title('Single-Sided Amplitude Spectrum of Half Rectified Sine Wave')
xlabel('Frequency (Hz)')
ylabel('Magnitude')

696 chars
33 lines

This code creates a half rectified sine wave in the time domain and then computes its Fourier transform to visualize the frequency content. Adjust the frequency f, time vector t, and amplitude A as needed.

related categories

gistlibby LogSnag