.unwrap()
In NumPy, the .unwrap()
function is used to unwrap a phase angle array. This function adjusts the phase angles by changing any absolute difference between consecutive angles greater than the specified threshold (default: pi
) by their 2*pi
complement. It is commonly used in signal processing to correct phase angle discontinuities in wrapped data.
Syntax
numpy.unwrap(p, discont=pi, axis=-1, period=6.283185307179586)
p
: The input array of phase angles to be unwrapped.discont
(optional): The discontinuity threshold, which defaults topi
. Differences greater than this value will be adjusted by adding or subtracting multiples of2*pi
.axis
(optional): The axis along which the unwrap operation is applied. Default is the last axis-1
.period
(optional): The period of the phase angles. Default is2*pi
, but it can be set to other values to adjust for different periodicities.
Example 1
This example demonstrates how to use the .unwrap()
function to correct phase discontinuities in an array of wrapped phase angles.
# Importing the 'numpy' library as 'np'import numpy as np# Creating an array of phase angles with discontinuitiesangles = np.array([0, np.pi/2, np.pi, -np.pi/2, -np.pi])# Applying the unwrap function to correct discontinuitiesunwrapped = np.unwrap(angles)print(unwrapped)
The above example code results in the following output:
[0. 1.57079633 3.14159265 4.71238898 3.14159265]
Note: NumPy outputs phase angles with high precision. For clarity, these examples use rounded values.
Example 2
This example shows how to adjust the discontinuity threshold by setting a custom discont value in .unwrap()
:
# Importing the 'numpy' library as 'np'import numpy as np# Creating an array of phase angles with large discontinuitiesangles = np.array([0, np.pi/2, np.pi, 3*np.pi, -np.pi/2])# Unwrapping with a custom discontinuity threshold of 2*piunwrapped = np.unwrap(angles, discont=2*np.pi)print(unwrapped)
The above example code results in the following output:
[0. 1.57079633 3.14159265 3.14159265 4.71238898]
Codebyte Example
In this Codebyte example, the .unwrap()
method corrects discontinuities in an array of phase angles:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours