# MATLAB - Amplitude and phase spectrum of a signal

82 views (last 30 days)
Randy Marsh on 22 Apr 2017
Commented: David Goodmanson on 13 Sep 2020
Plot an amplitude and a phase spectrum of the following signal s(t) in MATLAB
Signal is periodic.
Could someone show on this example how to plot amplitude and phase spectrum of any signal?
Also, what free pdf books of solved problems do you recommend for communications in MATLAB?
Where can I find solved MATLAB problems of this type?

Star Strider on 22 Apr 2017
You need the Symbolic Math Toolbox for this (unless you want to spend a few hours calculating it on your own).
The solution is to develop linear equations for each of the two ‘roof’ lines (the ‘house’ analogy is too tempting to ignore). These are the typical ‘y=m*t+b’ equations. You have all the information you need in the diagram to estimate the parameters and identify the equations.
Then take each equation (now only in ‘t’), multiply it by exp(1i*w*t) where ‘w’ is the radian frequency, and integrate the product with respect to ‘t’ over the region it’s defined. So the first integral would be from -1 to 0 (the positive slope line) and the second from 0 to +1 (the negative slope line). Then after you have evaluated the integrals (that are now expressions only in ‘w’), add the expressions together and simplify them. You should get some terms to cancel, and the others should simplify to trigonometric functions, using the Euler identities. Note that you can ignore the vertical lines (you do not need equations for them), since they are simply there for reference.
This should give you enough information to solve this. If you have problems, post what you have done, describe it and the problems, and leave a clear trail in your code so we can understand what you’ve done. We will probably be able to help.
I had to ask my professor how to approach this sort of problem when I first encountered them, so I understand that they are not obvious at first. You won’t have problems with the others you encounter after this.
Star Strider on 22 Apr 2017
I understand what you want. I’m not certain specifically where to refer you, although MathWorks Academia (link) is a very good place to begin. I would copy and paste the ISBNs of the books you’re interested in to a text file, and then do an online search to find out more about the books. The alibris (link) site is where I first go to purchase most books, especially technical books. (I have no financial or other interest in ‘alibris’.)
There are a number of books available on digital (or discrete) signal processing, Proakis being one, and Oppenheim being another popular option. (Both authors have different co-authors on different books.) I looked through my digital signal processing books and could not find any that specifically dealt with the problem you posted.
My graduate school course in basic signal processing had these problems as homework handouts. They weren’t in the textbook, which dealt with hardware circuit and filter analysis.
The analytic solution to the problem you posted is:
syms t w
ltz = 2 + 1*t; % Left Section (‘Less Than Zero’)
gtz = 2 - 1*t; % Right Section (‘Greater Than Zero’)
fourier_kernel = exp(1i*w*t); % Fourier Transform Kernel
F_ltz = int(ltz * fourier_kernel, t, -1, 0); % Fourier Transform OF ‘ltz’
F_gtz = int(gtz * fourier_kernel, t, 0, +1); % Fourier Transform OF ‘gtz’
F_signal = collect(F_ltz + F_gtz)
F_signal = rewrite(F_signal, 'sincos')
F_signal = simplify(F_signal, 'Steps',10)
I solved it in several steps so you can see how the idea evolves, as much as how the code works. (The code is therefore deliberately not efficient.) I will let you analyse the amplitude and phase (which, considering that the solution is purely real, is straightforward). Although the frequency components extend from -Inf to +Inf, I would plot it from -10*pi to +10*pi. That will give you a good idea of how the Fourier transform of your signal behaves.
EDIT
The code to plot it is:
figure(1)
fplot(F_signal, [-10*pi +10*pi])
grid
title(['\$' latex(subs(F_signal,'w','omega')) '\$'], 'Interpreter','latex')
The ‘amplitude’ is the absolute value of the Fourier transform of your signal. The ‘phase’ is defined as ‘atan2(imag(F_signal),real(F_signal))’.

Afshin Aghayan on 24 Jul 2017
Edited: Afshin Aghayan on 2 Aug 2017
look at the following Matlab function, it can calculate phase spectrum as well as amplitude spectrum with a perfect accuracy:
https://www.mathworks.com/matlabcentral/fileexchange/63965-amplitude-and-phase-spectra-of-a-signal--fourier-transform-
This program calculates amplitude and phase spectra of an input signal with acceptable accuracy especially in the calculation of phase spectrum.The code does three main jobs for calculation amplitude and phase spectra. First of all, it extends the input signal to infinity; because for calculation Fourier transform(FT) (fft function in Matlab), we consider our signal is periodic with an infinite wavelength, the code creates a super_signal by putting original signal next to itself until the length of super_signal is around 1000000 samples, why did I choose 1000000 samples? Actually, it is just based on try and error!! For most signals that I have tried, a supper signal with 1000000 samples has the best output.
Second, for calculating fft in Matlab you can choose different resolutions, the Mathwork document and help use NFFT=2^nextpow2(length(signal)), it definitely isn't enough for one that wants high accuracy output. Here, I choose the resolution of NFFT=100000 that works for most signals.
Third, the code filters result of FT by thresholding, it is very important step! For calculating phase spectrum, its result is very noisy because of floating rounding off error, it causes during calculation "arctan" even small rounding off error produces significant noise in the result of phase spectrum, for suppressing this kind of noise you can define a threshold value. It means if amplitude of specific frequency is less than predefined threshold value (you must define it) it put zero instead of it.
These three steps help to improve the result of amplitude and phase spectra significantly.
IF YOU USE THIS PROGRAM IN YOUR RESEARCH, PLEASE CITE THE FOLLOWING PAPER:
Afshin Aghayan, Priyank Jaiswal, and Hamid Reza Siahkoohi (2016). "Seismic denoising using the redundant lifting scheme." GEOPHYSICS, 81(3), V249-V260. https://doi.org/10.1190/geo2015-0601.1

Image Analyst on 22 Apr 2017
Try making the signal and taking the fft of it. Here's a start:
s = [1 : .01 : 2, 1.99 : -0.01 : 1]
t = linspace(-1, 1, length(s));
plot(t, s, 'LineWidth', 2);
grid on;
ylim([0, 3]);
s_spectrum = fft(s);
David Goodmanson on 13 Sep 2020
Hi Randy,
The plot of s(t) is slightly misleading. Since the signal is periodic, you can temporarily consider several of these 'houses' joined side by side. The end point of the first house is 1. The starting point of the second house is 1. That means that the vertical drop from 1 to 0 (and the vertical rise from 0 to 1) does not actually take place. Or, you could say that they do take place but cancel each other out where the houses are joined. So you end up with a series of roof lines that go back and forth from 1 to 2 and 2 to 1. To find the spectrum you will only need one house, but the starting and end points are 1, and there is no drop to 0.

Tahmidur Rahman on 7 Feb 2018