
- NumPy - Home
- NumPy - Introduction
- NumPy - Environment
- NumPy Arrays
- NumPy - Ndarray Object
- NumPy - Data Types
- NumPy Creating and Manipulating Arrays
- NumPy - Array Creation Routines
- NumPy - Array Manipulation
- NumPy - Array from Existing Data
- NumPy - Array From Numerical Ranges
- NumPy - Iterating Over Array
- NumPy - Reshaping Arrays
- NumPy - Concatenating Arrays
- NumPy - Stacking Arrays
- NumPy - Splitting Arrays
- NumPy - Flattening Arrays
- NumPy - Transposing Arrays
- NumPy Indexing & Slicing
- NumPy - Indexing & Slicing
- NumPy - Indexing
- NumPy - Slicing
- NumPy - Advanced Indexing
- NumPy - Fancy Indexing
- NumPy - Field Access
- NumPy - Slicing with Boolean Arrays
- NumPy Array Attributes & Operations
- NumPy - Array Attributes
- NumPy - Array Shape
- NumPy - Array Size
- NumPy - Array Strides
- NumPy - Array Itemsize
- NumPy - Broadcasting
- NumPy - Arithmetic Operations
- NumPy - Array Addition
- NumPy - Array Subtraction
- NumPy - Array Multiplication
- NumPy - Array Division
- NumPy Advanced Array Operations
- NumPy - Swapping Axes of Arrays
- NumPy - Byte Swapping
- NumPy - Copies & Views
- NumPy - Element-wise Array Comparisons
- NumPy - Filtering Arrays
- NumPy - Joining Arrays
- NumPy - Sort, Search & Counting Functions
- NumPy - Searching Arrays
- NumPy - Union of Arrays
- NumPy - Finding Unique Rows
- NumPy - Creating Datetime Arrays
- NumPy - Binary Operators
- NumPy - String Functions
- NumPy - Matrix Library
- NumPy - Linear Algebra
- NumPy - Matplotlib
- NumPy - Histogram Using Matplotlib
- NumPy Sorting and Advanced Manipulation
- NumPy - Sorting Arrays
- NumPy - Sorting along an axis
- NumPy - Sorting with Fancy Indexing
- NumPy - Structured Arrays
- NumPy - Creating Structured Arrays
- NumPy - Manipulating Structured Arrays
- NumPy - Record Arrays
- Numpy - Loading Arrays
- Numpy - Saving Arrays
- NumPy - Append Values to an Array
- NumPy - Swap Columns of Array
- NumPy - Insert Axes to an Array
- NumPy Handling Missing Data
- NumPy - Handling Missing Data
- NumPy - Identifying Missing Values
- NumPy - Removing Missing Data
- NumPy - Imputing Missing Data
- NumPy Performance Optimization
- NumPy - Performance Optimization with Arrays
- NumPy - Vectorization with Arrays
- NumPy - Memory Layout of Arrays
- Numpy Linear Algebra
- NumPy - Linear Algebra
- NumPy - Matrix Library
- NumPy - Matrix Addition
- NumPy - Matrix Subtraction
- NumPy - Matrix Multiplication
- NumPy - Element-wise Matrix Operations
- NumPy - Dot Product
- NumPy - Matrix Inversion
- NumPy - Determinant Calculation
- NumPy - Eigenvalues
- NumPy - Eigenvectors
- NumPy - Singular Value Decomposition
- NumPy - Solving Linear Equations
- NumPy - Matrix Norms
- NumPy Element-wise Matrix Operations
- NumPy - Sum
- NumPy - Mean
- NumPy - Median
- NumPy - Min
- NumPy - Max
- NumPy Set Operations
- NumPy - Unique Elements
- NumPy - Intersection
- NumPy - Union
- NumPy - Difference
- NumPy Random Number Generation
- NumPy - Random Generator
- NumPy - Permutations & Shuffling
- NumPy - Uniform distribution
- NumPy - Normal distribution
- NumPy - Binomial distribution
- NumPy - Poisson distribution
- NumPy - Exponential distribution
- NumPy - Rayleigh Distribution
- NumPy - Logistic Distribution
- NumPy - Pareto Distribution
- NumPy - Visualize Distributions With Sea born
- NumPy - Matplotlib
- NumPy - Multinomial Distribution
- NumPy - Chi Square Distribution
- NumPy - Zipf Distribution
- NumPy File Input & Output
- NumPy - I/O with NumPy
- NumPy - Reading Data from Files
- NumPy - Writing Data to Files
- NumPy - File Formats Supported
- NumPy Mathematical Functions
- NumPy - Mathematical Functions
- NumPy - Trigonometric functions
- NumPy - Exponential Functions
- NumPy - Logarithmic Functions
- NumPy - Hyperbolic functions
- NumPy - Rounding functions
- NumPy Fourier Transforms
- NumPy - Discrete Fourier Transform (DFT)
- NumPy - Fast Fourier Transform (FFT)
- NumPy - Inverse Fourier Transform
- NumPy - Fourier Series and Transforms
- NumPy - Signal Processing Applications
- NumPy - Convolution
- NumPy Polynomials
- NumPy - Polynomial Representation
- NumPy - Polynomial Operations
- NumPy - Finding Roots of Polynomials
- NumPy - Evaluating Polynomials
- NumPy Statistics
- NumPy - Statistical Functions
- NumPy - Descriptive Statistics
- NumPy Datetime
- NumPy - Basics of Date and Time
- NumPy - Representing Date & Time
- NumPy - Date & Time Arithmetic
- NumPy - Indexing with Datetime
- NumPy - Time Zone Handling
- NumPy - Time Series Analysis
- NumPy - Working with Time Deltas
- NumPy - Handling Leap Seconds
- NumPy - Vectorized Operations with Datetimes
- NumPy ufunc
- NumPy - ufunc Introduction
- NumPy - Creating Universal Functions (ufunc)
- NumPy - Arithmetic Universal Function (ufunc)
- NumPy - Rounding Decimal ufunc
- NumPy - Logarithmic Universal Function (ufunc)
- NumPy - Summation Universal Function (ufunc)
- NumPy - Product Universal Function (ufunc)
- NumPy - Difference Universal Function (ufunc)
- NumPy - Finding LCM with ufunc
- NumPy - ufunc Finding GCD
- NumPy - ufunc Trigonometric
- NumPy - Hyperbolic ufunc
- NumPy - Set Operations ufunc
- NumPy Useful Resources
- NumPy - Quick Guide
- NumPy - Cheatsheet
- NumPy - Useful Resources
- NumPy - Discussion
- NumPy Compiler
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.