What is the correct code for plotting an amplitude spectrum in MATLAB?

In summary, to plot the amplitude spectrum for a function with a period of 20, you can use the above code in MATLAB. It calculates the Fourier coefficients and then plots the amplitude spectrum using the stem function.
  • #1
magnifik
360
0
i am trying to plot the amplitude spectrum for a function which has the following properties:

f(t) = 1, 0 < t < 10
f(t) = 0, 10 < t < 20
the period of the function is 20

i found the Fourier coefficients to be Fn = -(1/n*pi)(cos(n*pi) - 1)

i tried to use the following code to plot the amplitude spectrum in MATLAB:
% up to the 10th harmonic
n = 10;
for n = -N:2:N,
cn = (-cos(n*pi)+1)/(n*pi); % Fourier coefficient
stem(n, abs(cn))
end

however, this plots a straight line with a circle at 10. I'm not sure how to fix this as i am a MATLAB newbie
 
Physics news on Phys.org
  • #2
! any help would be appreciated!If this is not the correct code to plot an amplitude spectrum, could someone please provide the correct code?The code you provided is for plotting the Fourier coefficients of the signal. To plot the amplitude spectrum, you need to use the following code:% up to the 10th harmonicN = 10;for n = -N:2:N,cn = (-cos(n*pi)+1)/(n*pi); % Fourier coefficientAmp(n+N+1) = abs(cn);endstem(-N:2:N, Amp)
 

1. What is a MATLAB amplitude spectrum?

A MATLAB amplitude spectrum is a graphical representation of the amplitude (or strength) of various frequency components in a signal. It is often used in signal processing and analysis to visualize and analyze the frequency content of a signal.

2. How do I plot an amplitude spectrum in MATLAB?

To plot an amplitude spectrum in MATLAB, you can use the fft function to compute the Fast Fourier Transform (FFT) of your signal, and then use the abs function to get the magnitude of the FFT. Finally, you can use the plot function to plot the magnitude against the frequency bins.

3. What is the difference between a one-sided amplitude spectrum and a two-sided amplitude spectrum?

A one-sided amplitude spectrum only displays the positive frequency components of a signal, while a two-sided amplitude spectrum shows both positive and negative frequency components. The one-sided spectrum is typically used when the signal is real-valued and symmetric, while the two-sided spectrum is used for complex-valued signals.

4. How do I interpret an amplitude spectrum in MATLAB?

In a MATLAB amplitude spectrum, the peaks represent the dominant frequencies in the signal, and the height of the peaks represents the strength or amplitude of those frequencies. The frequency at which the peak occurs is shown on the x-axis, while the amplitude is shown on the y-axis.

5. Can I customize the appearance of an amplitude spectrum plot in MATLAB?

Yes, you can customize the appearance of an amplitude spectrum plot in MATLAB by adjusting the axis limits, changing the color and style of the plot, adding labels and titles, and more. You can also use the subplot function to create multiple plots in one figure, allowing for easier comparison and analysis of different signals.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • Engineering and Comp Sci Homework Help
Replies
2
Views
812
  • Engineering and Comp Sci Homework Help
Replies
1
Views
949
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
934
  • Engineering and Comp Sci Homework Help
Replies
13
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
866
Back
Top