plot a rectified sine wave and plot its fourier coefficients in matlab

main.m
% Define parameters
N = 1000; % number of samples
fs = 1000; % sampling frequency (Hz)
f = 10; % frequency of the sine wave (Hz)
t = 0 : 1/fs : (N-1)/fs; % time vector

% Generate rectified sine wave
x = abs(sin(2*pi*f*t));

figure;
subplot(2,1,1);
plot(t, x);
title('Rectified Sine Wave');
xlabel('Time (s)');
ylabel('Amplitude');

% Calculate Fourier coefficients
X = fft(x)/N; % integral 1/N * Sum(x(t) * exp(-j*2*pi*f0*t)) dt
X_magnitude = abs(X); % magnitude of the coefficients
frequencies = 0 : fs/N : fs*(N-1)/N; % frequencies corresponding to the coefficients

subplot(2,1,2);
stem(frequencies, X_magnitude);
title('Fourier Coefficients');
xlabel('Frequency (Hz)');
ylabel('Magnitude');
696 chars
27 lines

This code generates a rectified sine wave, plots it, and then calculates and plots its Fourier coefficients.

gistlibby LogSnag