Blackman in Python Numpy


Introduction

In signal processing and spectral analysis, window functions play a vital part in forming and modifying signals. The Blackman window is a commonly utilized window function in signal processing that makes a difference decrease spectral leakage effects. It is actualized in Python utilizing the effective NumPy library, which gives effective cluster operations and scientific capacities.

In this article, we'll investigate three distinctive examples to execute the Blackman window in Python using the powerful NumPy library. Each approach will be went with by detailed calculations, step-by-step clarifications, of the Python syntax, and code cases with their respective outputs.

Blackman in Python Numpy

The Blackman window is a commonly utilized window function in signal processing that makes a difference decrease spectral leakage effects. It is actualized in Python utilizing the effective NumPy library, which gives effective cluster operations and scientific capacities.

To actualize the Blackman window in Python utilizing NumPy, you'll be able to utilize its vectorized operations. By taking advantage of these operations, you'll be able to calculate the window values for each test proficiently. The formula for the Blackman window is connected utilizing NumPy's built-in capacities, such as arranged for generating records and cos for computing the cosine values. The coming about window values are put away in a NumPy array, which can be effectively gotten to and controlled.

Example 1: Vectorized Operations using NumPy

The first approach utilizes NumPy's vectorized operations to efficiently compute the Blackman window values. Here's the algorithmic overview of this approach:

Algorithm

  • Step 1 − Import the NumPy library.

  • Step 2 − Create a function blackman_window(r) that contains one parameter N.

  • Step 3 − Use the formula: 0.42 - 0.5 * l.cos((2 * l.pi * t) / (r - 1)) + 0.08 * l.cos((4 * l.pi * t) / (r - 1)) to evaluate the Blackman window values.

  • Step 4 − Return the computed window as a NumPy array.

Program Code

import numpy as l
def blackman_window(r):
   t = l.arange(r)
   window = 0.42 - 0.5 * l.cos((2 * l.pi * t) / (r - 1)) + 0.08 * l.cos((4 * l.pi * t) / (r - 1))
   return window
# Example usage
wn_size = 100
blackman = blackman_window(wn_size)
print(blackman)

Output

[-1.38777878e-17  3.63046791e-04  1.45848970e-03  3.30508601e-03
  5.93359947e-03  9.38606173e-03  1.37147565e-02  1.89809442e-02
  2.52533489e-02  3.26064346e-02  4.11185013e-02  5.08696327e-02
  6.19395342e-02  7.44052977e-02  8.83391330e-02  1.03806108e-01
  1.20861937e-01  1.39550858e-01  1.59903635e-01  1.81935733e-01
  2.05645686e-01  2.31013696e-01  2.58000502e-01  2.86546517e-01
  3.16571288e-01  3.47973259e-01  3.80629867e-01  4.14397981e-01
  4.49114657e-01  4.84598234e-01  5.20649734e-01  5.57054572e-01
  5.93584531e-01  6.30000000e-01  6.66052422e-01  7.01486938e-01
  7.36045180e-01  7.69468167e-01  8.01499281e-01  8.31887262e-01
  8.60389188e-01  8.86773393e-01  9.10822277e-01  9.32334982e-01
  9.51129866e-01  9.67046769e-01  9.79949017e-01  9.89725134e-01
  9.96290257e-01  9.99587205e-01  9.99587205e-01  9.96290257e-01
  9.89725134e-01  9.79949017e-01  9.67046769e-01  9.51129866e-01
  9.32334982e-01  9.10822277e-01  8.86773393e-01  8.60389188e-01
  8.31887262e-01  8.01499281e-01  7.69468167e-01  7.36045180e-01
  7.01486938e-01  6.66052422e-01  6.30000000e-01  5.93584531e-01
  5.57054572e-01  5.20649734e-01  4.84598234e-01  4.49114657e-01
  4.14397981e-01  3.80629867e-01  3.47973259e-01  3.16571288e-01

Example 2: List Comprehension

The second approach employs list comprehension to create the Blackman window values. Here's the algorithmic overview of this approach −

Algorithm

  • Step 1 − Import the specified library.

  • Step 2 − Create a function named blackman_window(N).

  • Step 3 − Compute the Blackman window values using the formula within the list comprehension.

  • Step 4 − Convert the resulting list into a NumPy array.

  • Step 5 − Return the computed window.

Program Code

import numpy as r

def blackman_window(t):
   window = [0.42 - 0.5 * r.cos((2 * r.pi * n) / (t - 1)) + 0.08 * r.cos((4 * r.pi * n) / (t - 1)) for n in range(t)]
   return r.array(window)

# Example usage
window_size = 100
blackman = blackman_window(window_size)
print(blackman)

Output

[-1.38777878e-17  3.63046791e-04  1.45848970e-03  3.30508601e-03
  5.93359947e-03  9.38606173e-03  1.37147565e-02  1.89809442e-02
  2.52533489e-02  3.26064346e-02  4.11185013e-02  5.08696327e-02
  6.19395342e-02  7.44052977e-02  8.83391330e-02  1.03806108e-01
  1.20861937e-01  1.39550858e-01  1.59903635e-01  1.81935733e-01
  2.05645686e-01  2.31013696e-01  2.58000502e-01  2.86546517e-01
  3.16571288e-01  3.47973259e-01  3.80629867e-01  4.14397981e-01
  4.49114657e-01  4.84598234e-01  5.20649734e-01  5.57054572e-01
  5.93584531e-01  6.30000000e-01  6.66052422e-01  7.01486938e-01
  7.36045180e-01  7.69468167e-01  8.01499281e-01  8.31887262e-01
  8.60389188e-01  8.86773393e-01  9.10822277e-01  9.32334982e-01
  9.51129866e-01  9.67046769e-01  9.79949017e-01  9.89725134e-01
  9.96290257e-01  9.99587205e-01  9.99587205e-01  9.96290257e-01
  9.89725134e-01  9.79949017e-01  9.67046769e-01  9.51129866e-01
  9.32334982e-01  9.10822277e-01  8.86773393e-01  8.60389188e-01
  8.31887262e-01  8.01499281e-01  7.69468167e-01  7.36045180e-01
  7.01486938e-01  6.66052422e-01  6.30000000e-01  5.93584531e-01
  5.57054572e-01  5.20649734e-01  4.84598234e-01  4.49114657e-01
  4.14397981e-01  3.80629867e-01  3.47973259e-01  3.16571288e-01

Example 3: Using NumPy's from function method

The third approach utilizes NumPy's fromfunction method, which allows us to create an array by executing a function over each coordinate. Here's the algorithmic overview of this approach −

Algorithm

  • Step 1 − Import the required module.

  • Step 2 − Creation of a function blackman_window(N) that takes the window size N as a parameter.

  • Step 3 − Define an inner function blackman_func(n) that calculates the Blackman window value based on the given index n.

  • Step 4 − Use np.fromfunction to create the window array by applying the blackman_func function to each coordinate.

  • Step 5 − Return the computed window.

Program Code

import numpy as np

def blackman_window(N):
   def blackman_func(n):
      return 0.42 - 0.5 * np.cos((2 * np.pi * n) / (N - 1)) + 0.08 * np.cos((4 * np.pi * n) / (N - 1))
    
   window = np.fromfunction(blackman_func, (N,))
   return window

# Example usage
window_size = 100
blackman = blackman_window(window_size)
print(blackman)

Output

[-1.38777878e-17  3.63046791e-04  1.45848970e-03  3.30508601e-03
  5.93359947e-03  9.38606173e-03  1.37147565e-02  1.89809442e-02
  2.52533489e-02  3.26064346e-02  4.11185013e-02  5.08696327e-02
  6.19395342e-02  7.44052977e-02  8.83391330e-02  1.03806108e-01
  1.20861937e-01  1.39550858e-01  1.59903635e-01  1.81935733e-01
  2.05645686e-01  2.31013696e-01  2.58000502e-01  2.86546517e-01
  3.16571288e-01  3.47973259e-01  3.80629867e-01  4.14397981e-01
  4.49114657e-01  4.84598234e-01  5.20649734e-01  5.57054572e-01
  5.93584531e-01  6.30000000e-01  6.66052422e-01  7.01486938e-01
  7.36045180e-01  7.69468167e-01  8.01499281e-01  8.31887262e-01
  8.60389188e-01  8.86773393e-01  9.10822277e-01  9.32334982e-01
  9.51129866e-01  9.67046769e-01  9.79949017e-01  9.89725134e-01
  9.96290257e-01  9.99587205e-01  9.99587205e-01  9.96290257e-01
  9.89725134e-01  9.79949017e-01  9.67046769e-01  9.51129866e-01
  9.32334982e-01  9.10822277e-01  8.86773393e-01  8.60389188e-01
  8.31887262e-01  8.01499281e-01  7.69468167e-01  7.36045180e-01
  7.01486938e-01  6.66052422e-01  6.30000000e-01  5.93584531e-01
  5.57054572e-01  5.20649734e-01  4.84598234e-01  4.49114657e-01
  4.14397981e-01  3.80629867e-01  3.47973259e-01  3.16571288e-01

Conclusion

In this article, we investigated three diverse approaches to implementing the Blackman window in Python utilizing the NumPy library. We secured the calculations, step-by-step clarifications, Python language structure, and code cases with their individual yields for each approach. The primary approach utilized NumPy's vectorized operations, the moment approach utilized list comprehension, and the third approach utilized NumPy's fromfunction strategy.

Updated on: 25-Aug-2023

121 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements