Python NumPy angle Function

The angle function in Python's NumPy library is used to compute the angle (or phase) of complex numbers in radians. This function is essential in various fields such as signal processing, control systems, and scientific computing where the phase information of complex numbers is required.

Table of Contents

  1. Introduction
  2. Importing the numpy Module
  3. angle Function Syntax
  4. Understanding angle
  5. Examples
    • Basic Usage
    • Applying angle to Arrays
    • Handling Special Values
  6. Real-World Use Case
  7. Conclusion
  8. Reference

Introduction

The angle function in Python's NumPy library allows you to compute the angle (or phase) of complex numbers in an array. This function is particularly useful in numerical computations where phase information of complex numbers is necessary.

Importing the numpy Module

Before using the angle function, you need to import the numpy module, which provides the array object.

import numpy as np

angle Function Syntax

The syntax for the angle function is as follows:

np.angle(z, deg=False)

Parameters:

  • z: The input array of complex numbers.
  • deg: Optional. If True, the angle is returned in degrees. Default is False, which returns the angle in radians.

Returns:

  • An array with the angles (or phases) of the complex numbers in the input array.

Understanding angle

The angle function computes the counterclockwise angle from the positive real axis on the complex plane to the line segment that joins the point to the origin. The angle is measured in radians by default, but can be returned in degrees if specified.

Examples

Basic Usage

To demonstrate the basic usage of angle, we will compute the angle of a single complex number.

Example

import numpy as np

# Complex number
z = 1 + 1j

# Computing the angle in radians
angle_rad = np.angle(z)
print(f"Angle in radians: {angle_rad}")

# Computing the angle in degrees
angle_deg = np.angle(z, deg=True)
print(f"Angle in degrees: {angle_deg}")

Output:

Angle in radians: 0.7853981633974483
Angle in degrees: 45.0

Applying angle to Arrays

This example demonstrates how to apply the angle function to an array of complex numbers.

Example

import numpy as np

# Array of complex numbers
z = np.array([1 + 1j, 1 - 1j, -1 + 1j, -1 - 1j])

# Computing the angles in radians
angles_rad = np.angle(z)
print(f"Angles in radians: {angles_rad}")

# Computing the angles in degrees
angles_deg = np.angle(z, deg=True)
print(f"Angles in degrees: {angles_deg}")

Output:

Angles in radians: [ 0.78539816 -0.78539816  2.35619449 -2.35619449]
Angles in degrees: [  45.  -45.  135. -135.]

Handling Special Values

This example demonstrates how angle handles special values such as purely real or purely imaginary numbers.

Example

import numpy as np

# Array of special complex numbers
z = np.array([1, 1j, -1, -1j])

# Computing the angles in radians
angles_rad = np.angle(z)
print(f"Angles in radians: {angles_rad}")

# Computing the angles in degrees
angles_deg = np.angle(z, deg=True)
print(f"Angles in degrees: {angles_deg}")

Output:

Angles in radians: [ 0.          1.57079633  3.14159265 -1.57079633]
Angles in degrees: [  0.  90. 180. -90.]

Real-World Use Case

Signal Processing: Phase Information

In signal processing, the angle function can be used to extract the phase information of a complex signal, which is crucial for understanding the signal's characteristics.

Example

import numpy as np

# Example complex signal
signal = np.array([1 + 1j, 0.707 + 0.707j, 0 + 1j, -0.707 + 0.707j])

# Extracting phase information in degrees
phase_info = np.angle(signal, deg=True)
print(f"Phase Information: {phase_info}")

Output:

Phase Information: [ 45.  45.  90. 135.]

Conclusion

The angle function in Python's NumPy library is used for computing the angle (or phase) of complex numbers in an array. This function is useful in various numerical and data processing applications, particularly those involving complex numbers and signal processing. Proper usage of this function can enhance the accuracy and efficiency of your computations.

Reference

Python NumPy angle Function

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare