Plot the phase spectrum in Python using Matplotlib - GeeksforGeeks

27 Apr, 2020
A Signal is an electromagnetic field or an electric current to transmit data. There are various components of a signal such as frequency, amplitude, wavelength, phase, angular frequency and period from which it is described.A periodic signal can be represented using the below sine function:
y = A sin(w*t + Q)

In which A represents the amplitude(in meter), w represents frequency(in hertz), t represents time period(in seconds) and Q represents phase(in radian) of the periodic signal.
The two major components frequency and phase of a periodic signal define the Phase Spectrum of that signal. The frequency components of the periodic signal are plotted in the horizontal axis and phase component of the periodic signal is plotted in the vertical axis.
In Python, the phase_spectrum() method in the pyplot module of Python matplotlib library plots the phase spectrum of a periodic signal. Below are some programs which demonstrate the use of phase_spectrum() method to visualize the phase spectrum of different periodic signals.Example 1:
# importing modulesimport numpyfrom matplotlib import pyplot    # assigning time values of the signal# initial time period, final time period and phase anglesignalTime = numpy.arange(5, 10, 0.25);  # getting the amplitude of the signalsignalAmplitude = numpy.sin(signalTime)  # plotting the signal pyplot.plot(signalTime, signalAmplitude, color ='green')pyplot.show()  pyplot.xlabel('Time')pyplot.ylabel('Amplitude')pyplot.title("Signal")    # plotting the phase spectrum of the signal pyplot.phase_spectrum(signalAmplitude, color ='green')  pyplot.title("Phase Spectrum of the Signal")pyplot.show()
Output:The first graph represent the signal in Amplitude vs Time components, the second graph represents the phase spectrum of the signal in Phase vs Frequency graph by using phase_spectrum() on the signal having time period from 5 to 10 seconds, 0.25 radian phase angle, frequency of the signal is calculated from the given time period and amplitude of the signal is calculated using the sin() function in numpy module.Example 2:
# importing modulesimport numpyfrom matplotlib import pyplot    # assigning time values of the signal# initial time period, final time period and phase anglesignalTime = numpy.arange(0, 1, 0.1)  # getting the amplitude of the signalsignalAmplitude = numpy.sin(signalTime)  # plotting the signal pyplot.plot(signalTime, signalAmplitude, color ='green')pyplot.show()  pyplot.xlabel('Time')pyplot.ylabel('Amplitude')pyplot.title("Signal")    # plotting the phase spectrum of the signal pyplot.phase_spectrum(signalAmplitude, color ='green')  pyplot.title("Phase Spectrum of the Signal")pyplot.show()
Output:In the above program, as the amplitude of the signal is increasing with time so a sinusoidal wave is not formed in the first graph. The signal exists in the time period of 0 to 1 second and the phase angle is 0.1 radian, the phase spectrum of the signal is depicted using phase_spectrum() method.
Example 3:
# importing modulesimport numpyfrom matplotlib import pyplot    # assigning time values of the signal# initial time period, final time period and phase angle signalTime = numpy.arange(1, 100, 0.5);  # getting the amplitude of the signalsignalAmplitude = numpy.sin(signalTime)  # plotting the signal pyplot.plot(signalTime, signalAmplitude, color ='green')pyplot.show()  pyplot.xlabel('Time')pyplot.ylabel('Amplitude')pyplot.title("Signal")    # plotting the phase spectrum of the signal pyplot.phase_spectrum(signalAmplitude, color ='green')  pyplot.title("Phase Spectrum of the Signal")pyplot.show()
Output:Here, the signal is represented in Amplitude vs Time graph which forms sinusoidal waves and the phase spectrum of the signal is represented using phase_spectrum() method in Phase vs Frequency graph. The time period of the signal starts from 1 second to 100th second and the phase angle is 0.5 radian.
Python-matplotlib
Python
Write From Home
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python Dictionary
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe
Convert integer to string in Python
Convert string to integer in Python
How to set input type date in dd-mm-yyyy format using HTML ?
Python infinity
Matplotlib.pyplot.title() in Python