NumPy - Signal Processing Applications



NumPy Signal Processing

Signal processing in NumPy involves manipulating and analyzing signals, which are functions that convey information. These signals can be anything from sound waves to digital data. The goal is to transform or extract useful information from these signals.

NumPy provides tools for signal processing, such as filtering, Fourier transforms, and convolution. For example, you can use Fourier transforms to convert signals from the time domain to the frequency domain, making it easier to analyze their frequency components.

You can also apply filters to remove noise or extract specific parts of a signal. These operations are useful in various fields, including audio processing, communications, and image analysis.

Filtering Signals

Filtering is a fundamental signal processing technique used to remove unwanted components from a signal or to extract useful parts. Filters can be low-pass, high-pass, band-pass, or band-stop, depending on the frequencies they allow or block.

Example: Low-Pass Filtering

In the following example, we apply a low-pass filter to a noisy signal to remove high-frequency noise −

import numpy as np
from scipy.signal import butter, lfilter

# Generate a sample signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t) + np.random.randn(500) * 0.5

# Design a low-pass filter
def butter_lowpass(cutoff, fs, order=5):
   nyquist = 0.5 * fs
   normal_cutoff = cutoff / nyquist
   b, a = butter(order, normal_cutoff, btype='low', analog=False)
   return b, a

def lowpass_filter(data, cutoff, fs, order=5):
   b, a = butter_lowpass(cutoff, fs, order=order)
   y = lfilter(b, a, data)
   return y

# Apply the low-pass filter
cutoff_frequency = 10
sampling_rate = 500
filtered_signal = lowpass_filter(signal, cutoff_frequency, sampling_rate)

print("Filtered signal:", filtered_signal)

The result shows the filtered signal with high-frequency noise removed −

Filtered signal: [ 3.92381804e-07  3.54383602e-06  1.63967023e-05  5.28930079e-05 ... -9.55624060e-01 -9.81063288e-01 -1.00076423e+00]

Fourier Transform for Frequency Analysis

Fourier Transform is widely used in signal processing to analyze the frequency components of a signal. It converts a time-domain signal into its frequency-domain representation, revealing the frequencies present in the signal.

Example: Frequency Analysis Using FFT

In the following example, we use the Fast Fourier Transform (FFT) to analyze the frequency components of a signal −

import numpy as np

# Generate a sample signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.sin(2 * np.pi * 10 * t)

# Compute the FFT
fft_values = np.fft.fft(signal)

# Compute the frequency bins
freqs = np.fft.fftfreq(len(signal), d=t[1] - t[0])

print("Frequency components:", freqs)
print("FFT values:", fft_values)

The result shows the frequency components and their corresponding FFT values −

Frequency components: [   0.    1.    2.    3.    4.    5. ...]
FFT values: [-3.87326339e-14+0.00000000e+00j  4.12725409e-14+2.68673972e-14j ...]

Convolution and Correlation

Convolution and correlation are mathematical operations used in signal processing to analyze and modify signals. Convolution is used to filter signals, while correlation measures the similarity between signals.

Example: Convolution

In the following example, we use convolution to apply a smoothing filter to a signal −

import numpy as np

# Generate a sample signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t) + np.random.randn(500) * 0.5

# Define a smoothing filter
filter_kernel = np.ones(10) / 10

# Apply convolution
smoothed_signal = np.convolve(signal, filter_kernel, mode='same')

print("Smoothed signal:", smoothed_signal)

The result shows the smoothed signal after applying convolution −

Smoothed signal: [ 0.10531384  0.10089093  0.10978193 ... -0.19290414-0.26696232 -0.2166795 ]

Example: Cross-Correlation

In the following example, we compute the cross-correlation between two signals −

import numpy as np

# Generate two sample signals
t = np.linspace(0, 1, 500, endpoint=False)
signal1 = np.sin(2 * np.pi * 5 * t)
signal2 = np.sin(2 * np.pi * 5 * t + np.pi / 4)

# Compute the cross-correlation
cross_corr = np.correlate(signal1, signal2, mode='full')

print("Cross-correlation:", cross_corr)

The result shows the cross-correlation between the two signals −

Cross-correlation: [ 0.00000000e+00  4.15241156e-02  1.21369107e-01 ... -2.76126688e-01 -1.35723843e-01 -4.43996022e-02]

Signal Resampling

Resampling involves changing the sampling rate of a signal, either increasing (upsampling) or decreasing (downsampling) the number of samples. This is often required when working with signals at different sampling rates.

Example: Signal Resampling

In the following example, we downsample a signal by a factor of 2 −

import numpy as np
from scipy.signal import resample

# Generate a sample signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t)

# Downsample the signal by a factor of 2
num_samples = len(signal) // 2
downsampled_signal = resample(signal, num_samples)

print("Downsampled signal:", downsampled_signal)

The result shows the downsampled signal −

Downsampled signal: [-3.51535300e-16  1.25333234e-01  2.48689887e-01 ... -2.48689887e-01 -1.25333234e-01]

Wavelet Transform

Wavelet Transform is another technique for analyzing signals, especially for non-stationary signals where frequency components vary over time. Wavelet Transform provides a time-frequency representation of the signal.

Example: Continuous Wavelet Transform

In the following example, we use the Continuous Wavelet Transform (CWT) to analyze a signal −

import numpy as np
import pywt

# Generate a sample signal
t = np.linspace(0, 1, 500, endpoint=False)
signal = np.sin(2 * np.pi * 5 * t) + 0.5 * np.sin(2 * np.pi * 10 * t)

# Compute the Continuous Wavelet Transform
coeffs, freqs = pywt.cwt(signal, scales=np.arange(1, 128), wavelet='gaus1')

print("CWT coefficients:", coeffs)
print("Frequencies:", freqs)

The result shows the CWT coefficients and corresponding frequencies −

CWT coefficients: [[ 5.11544621e-01  5.51952738e-01  5.90635494e-01 ...  6.21499677e-01  5.95101447e-01  5.67801389e-01]
 [ 1.08933167e+00  1.07658265e+00  1.05980188e+00 ...  9.62940518e-01  9.99652239e-01  1.03508272e+00]
 ...
 [-5.28687791e-04 -4.50587872e-04 -3.71247724e-04 ... -1.84740494e-04 -3.07220733e-04 -4.29412055e-04]]
Frequencies: [ 0.03125     0.0625      0.09375    ...  1.65625     1.6875      1.71875]

Applications of Signal Processing

Signal processing has numerous applications in various fields, such as −

  • Audio processing: Noise reduction, equalization, and compression in music and speech signals.
  • Image processing: Enhancement, filtering, and compression of digital images.
  • Communications: Modulation, demodulation, and error correction in transmission systems.
  • Control systems: Signal conditioning and feedback control in engineering systems.
  • Biomedical engineering: Analysis and filtering of physiological signals such as ECG and EEG.
Advertisements