How to plot the frequency spectrum of a signal on Matlab?

Hello everybody I have already developed a small code with a lot of help from Mathworks and the help function (in Matlab), and from some forums but I need someone to help me with my code. It's pretty much working but the spectrum's amplitude is not going according to theory. It should be half of the carrier's amplitude which in my case should be 25 volts, but Matlab's plot gives 22.51 volts which is not correct also on the sidebands I am getting 8.476 volts and it should be a quarter of the carrier's amplitude which in my case is 12.5 volts.
Here is my code:
clear all;clc
Fs = 200; % Sampling frequency Fs >> 2fmax & fmax = 50 Hz
t = 0:1/Fs:7501; % length (x) = 7501
x = 50*(1+0.75*sin(2*pi*t)).*cos(100*pi*t); % AM Signal
xdft = (1/length(x)).*fft(x);
freq = -100:(Fs/length(x)):100-(Fs/length(x)); %Frequency Vector
y = abs(fftshift(xdft));
figure(1); % Plot signals in both time and frequency domain
subplot(211);plot(t,x);grid on;title 'Amplitude Modulated Signal';
axis([0 5 -100 100]);xlabel 'Time (sec)';ylabel 'Amplitude (volts)';
subplot(212);plot(freq,y);grid on;title 'AM Signal Spectrum';
axis([-60 60 0 25]);xlabel 'Frequency (Hz)';ylabel 'Amplitude (volts)';

2 Comments

What is the problem with your code?
The code is apparently working but it is not giving me the correct values in the spectrum's amplitude and I don't know how why or how to fix it, so far it is the best I could find for plotting the frequency domain of a signal.

Sign in to comment.

 Accepted Answer

Before with my time vector wrong t = 0:1/Fs:7501; % length (x) = 7501 <http://i920.photobucket.com/albums/ad48/gabrielthemessenger/before.jpg>
After with the correct time vector t = 0:1/Fs:1-1/Fs; <http://i920.photobucket.com/albums/ad48/gabrielthemessenger/after.jpg>

6 Comments

what do you mean by upper and lower sidebands dissapeared , should they exist?
yes they should, since this is Amplitude Modulation like Radio Theory(Communications) the upper an lower sidebands contain the information signal the middle one is just the carrier a frequency and power, used to propagate the information through the air.
Thank you for your help it was very useful, I spoke with my Communications Professor and he said that it's a very good approximation for the values ~= 10 intead of 12.5 it is acceptable for a simulation. So thank you I'll post my final code for anyone to use Thanks again! :).
Code: clear all;clc Fs = 200; % Sampling frequency Fs >> 2fmax & fmax = 50 Hz. t = 0:1/Fs:1-1/Fs; % Time vector x = 50*(1+0.75*sin(2*pi*t)).*cos(100*pi*t); % AM Signal xdft = (1/length(x)).*fft(x); freq = -100:(Fs/length(x)):100-(Fs/length(x)); % Frequency Vector must be proportional to Fs y = abs(fftshift(xdft)); figure(1); % Plot signals in both time and frequency domain subplot(211);plot(t,x);grid on;title 'Amplitude Modulated Signal'; axis([0 1 -100 100]);xlabel 'Time (sec)';ylabel 'Amplitude (volts)'; subplot(212);stem(freq,y);grid on;title 'AM Signal Spectrum'; axis([-60 60 0 25]);xlabel 'Frequency (Hz)';ylabel 'Amplitude(volts)';
Gabriel, you've accepted your own answer which is not an answer. You should accept my answer, that helped you to find your mistakes!
Sorry I don't know how to undo it :(

Sign in to comment.

More Answers (1)

The frequency of your signal is F=1 Hz, Then why t = 0:1/Fs:7501
I suggest
t = 0:1/Fs:1-1/Fs

8 Comments

Great it gives me the correct value for the amplitude at the carrier (50 Hz), but for some strange reason the upper and lower sidebands dissapeared :( but thanks anyway! The time vector was definitely wrong.
Take a look at the images below. It should be three impulses at 50 Hz, 51 Hz & 49 Hz and a repetition in the negative side. The amplitude of the 50 Hz impulse should be 25 volts as is with the fixed time vector, but the other impulses disappeared they should have an amplitude of 12.5 volts.
Don't use plot for the frequency response use instead
stem(freq,y)
the plot just joined the three impulse
After stem: <http://i920.photobucket.com/albums/ad48/gabrielthemessenger/after_stem.jpg> The impulses appeared but their amplitude is still wrong they should be 12.5 volts. But we are getting there :)
I know, I have your code. But what is the problem now? The amplitudes are ~=10 and not 12.5, you can't get 12.5
why should they be 12.5? I have also, used my code and get the same result then you
Because the theory establishes that the amplitude of those frequency components should be half the carrier's amplitude in my case the carrier's amplitude is 50 volts so when it is transformed into the frequency domain the amplitude decreases by half on the carrier representation in the frequency domain so it should be 25 volts on the middle impulse (carrier) & one quarter of the carrier's amplitude on both the upper & lower impulses so 50/4 = 12.5 volts.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!