create half-rectified sine plots both in time and in frequency with fourier coefficients in matlab

main.m
% Generate a half-rectified sine wave
fs = 1000; % Sampling frequency in Hz
t = 0:1/fs:1; % Time vector
f = 10; % Frequency of the sine wave in Hz
x = sin(2*pi*f*t); % Generate a sine wave
x(x<0) = 0; % Half-rectify the sine wave

% Time domain plot
figure;
plot(t,x);
xlabel('Time (s)');
ylabel('Amplitude');
title('Half-rectified Sine Wave in Time Domain');

% Fourier Transform
N = length(x); % Length of the signal
X = fft(x,N)/N; % Compute the Fourier Transform
X_mag = abs(X); % Magnitude spectrum
frequencies = linspace(0, fs, N); % Frequency axis

% Frequency domain plot
figure;
stem(frequencies, X_mag);
xlabel('Frequency (Hz)');
ylabel('Magnitude');
title('Half-rectified Sine Wave in Frequency Domain');

% Display Fourier Coefficients
disp('Fourier Coefficients:');
disp(X);
788 chars
31 lines

gistlibby LogSnag