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

main.m
% Specify the frequency of the sine wave
f = 1; 

% Specify the time range
t = 0:0.01:1; 

% Create the sine wave
x = sin(2*pi*f*t);

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

% Compute the Fourier coefficients
N = length(x);
X = fft(x)/N;
frequencies = 0:1/N:1-1/N;

% Plotting the sine wave in frequency domain
subplot(2,1,2);
stem(frequencies, abs(X));
xlabel('Frequency');
ylabel('Amplitude');
title('Sine Wave in Frequency Domain');
537 chars
28 lines

In this code snippet, we create a half-wave sine plot in both time and frequency domain using MATLAB. We first specify the frequency of the sine wave, create the sine wave signal, and then plot it in the time domain. We then compute the Fourier coefficients using the Fast Fourier Transform (FFT) and plot the sine wave in the frequency domain.

related categories

gistlibby LogSnag