Table of Contents
ToggleSignal 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 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.
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)
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.
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)
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.
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)
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)
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.
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)
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.
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)
Content provided by Vista Academy
