plot a rectified half sine wave in time and plot its spectrum of the first seven fourier coefficients in matlab

main.m
% Generating a rectified half sine wave
t = linspace(0, 2*pi, 1000);  % time vector
y = abs(sin(t));  % rectified half sine wave

% Plotting the rectified half sine wave in time domain
figure;
subplot(2,1,1);
plot(t, y);
title('Rectified Half Sine Wave in Time Domain');
xlabel('Time');
ylabel('Amplitude');

% Performing Fourier Transform to get the spectrum
N = 7;  % Number of Fourier coefficients
dt = t(2) - t(1);  % Time step
Fs = 1/dt;  % Sampling frequency
Y = fft(y) / length(y);  % Fourier Transform
frequencies = Fs*(0:(length(y)/2))/length(y);

% Plotting the spectrum of the first seven Fourier coefficients
subplot(2,1,2);
stem(frequencies(1:N), abs(Y(1:N)));
title('Spectrum of first seven Fourier coefficients');
xlabel('Frequency');
ylabel('Magnitude');
771 chars
26 lines
main.m
0 chars
1 lines

gistlibby LogSnag