Python Artificial Intelligence in Practice: Intelligent Signal Processing

1. Background Introduction

At present, artificial intelligence technology has entered the field of automation and achieved remarkable results. It has made breakthrough progress in the fields of image recognition, speech recognition, natural language processing and other fields, but it has not received such attention in the field of signal processing. For digital signal processing, many traditional methods cannot extract features and information well. Therefore, how to improve the accuracy, robustness, operating efficiency, and reduce computational complexity of digital signal processing will become an important research direction in the field of artificial intelligence. Due to the need to improve traditional algorithms, signal processing is increasingly used in artificial intelligence technology. This article will give a comprehensive explanation of digital signal processing by combining the latest machine learning, deep learning technology and traditional signal processing methods, and implement some specific cases based on the open source toolkit Scikit-Learn.

2. Core concepts and connections

Digital signal processing (Digital Signal Processing) refers to the general term for the use of computers to sample, filter, encode, quantize, process, store, transmit, receive, analyze, display, etc. analog or digital signals. Digital signal processing technology is a technology that enables signals to have specific functions during propagation. It is a subject field closely related to computing technology and is the product of the cross-fusion of electronic information, electrical engineering, control engineering, computer science and communication engineering. Due to the wide application of signal processing technology, the analysis and design of signal processing is also a necessary skill. Commonly used digital signal processing methods mainly include time-varying processing, frequency domain processing, spectral analysis, statistical learning methods, neural networks, machine learning, feature extraction, etc.

In this article, we will provide an introduction to digital signal processing around three main topics:

  1. Time-varying processing: Analysis and filtering of signals through time or correlation.
  2. Frequency domain processing: Analyze and filter signals through time-frequency response functions.
  3. Mixed signal processing: Mixed signals are generated from multiple signal sources and require signal processing to obtain meaningful information.

3. Time-varying processing

Time-varying processing is to analyze and filter signals through time or correlation, such as transforming signals through periodicity, Fourier transform, Fourier-level transform, etc., finding the spectrum of the signal, detecting frequency jumps, detecting edges, and estimating signals Spectrum.

1) Time Domain Filter

Time domain filters are the simplest and most common filter type. The basic idea is to give a standard function $\delta(t)$, and then through linear convolution, the input signal $x(t)$ can be smoothed, delayed or extended for a certain time. Among them, $\delta(t)=\left{ \begin{array}{c} 1 & amp; t>T_p \ 0 & amp; others \end{array} \right.$ , which means The signal before the cut-off time $T_p$ is 1, and the signal after it is 0. In general, $\delta(t)$ is a sine function or cosine function with respect to time. In this way, after multiplying the signal $x(t)$ by $\delta(t)$, $x(t)$ whose absolute value is less than $\frac{1}{\sqrt{N}}$ can be eliminated ingredients to achieve a smooth effect.

$$ y[n]=\sum_{k=-\infty}^{\infty} x[k] \delta[n-k]\tag{1}\label{eq:1} $$

Among them, $n$ is the sampling point serial number, and $k$ is the corresponding sampling point serial number.

2) Filter bank and continuous time system

A filter bank refers to a series connection of multiple time domain filters, or there is a certain relationship between them, such as their coefficients transmitting to each other and interacting with each other. The time domain filter bank can be understood as a continuous time system. Its input and output are continuous signals, and the operations in the intermediate process are discrete.

Time-domain filter banks are easier to analyze, design, and debug and can be completed quickly, but they do not adapt well to changing signals. For changing signals, even if they can be smoothed with time domain filters, certain distortion will be introduced. Therefore, if you want the filter to have better adaptability and robustness, you need to adopt another more powerful time-varying processing method-the filter design method.

3) Best universal FIR filter

Optimal FIR filter (Optimal FIR filter) is one of the time domain filter banks. It is a kind of FIR filter. Its design goal is to minimize the residual sequence, so the usual best choice is to solve a corresponding optimization problem. Unlike ordinary FIR filters, this filter can be set at any order, and its flat spectrum region has a maximum value at the center of the signal.

The best general-purpose FIR filter can be solved using the Lagrange multiplier method or the Karhunen-Loeve transform method. The former is used for small-scale problems, and the latter is used for large-scale problems.

4) IIR filter

The IIR filter (Infinite Impulse Response Filter) is a time domain filter designed to capture signals containing time uncertainty. Compared with the best general-purpose FIR filter, the IIR filter can provide a longer delay and can adaptively adjust the filter order according to the characteristics of the signal, so that it can handle non-uniform channels well. However, compared with time domain FIR filters, the performance of IIR filters often depends on the noise power spectrum within the cut-off time.

Typical IIR filters include Butterworth bandpass filter, Butterworth low-pass filter, Laplacian sawtooth window filter, Hanning window filter, etc.

5) Window function method

The window function method is a time domain filter design method that uses a window function to smooth the signal, thereby reducing the degree of filter distortion. Compared with other time domain filter design methods, the window function method does not require an accurate frequency response function, which can simplify the design difficulty and can also obtain good performance within a certain range.

6) Comparison and selection of various common filters

Time domain FIR filter Time domain IIR filter
Order Variable Immutable
Delay Yes Relatively short Long delay
Filter mode Rectangular window Zigzag
Design method Lagrange multiplier method, Karhunen-Loeve Lagrange transform, signal decomposition
Error Small Large
Filter flat spectrum area Center The brightest place The center is darker

4. Frequency domain processing

Frequency domain processing is to analyze and filter signals through time-frequency response functions. These functions reflect the characteristics of signals that change with frequency. In the filter design process, frequency domain filters can improve the robustness and adaptability of the filter, thereby improving the efficiency and accuracy of signal processing.

1) Low Pass Filter

Low Pass Filter, also known as low-pass filter, slow damping filter, damping function filter, and zero-crossing lag filter. Its basic principle is to retain only low-frequency signals by blocking signals with too high frequencies. This kind of filter can usually effectively offset the influence of high-frequency signals by detecting low-frequency signals, and has a strong protective effect on changes in the entire signal.

Its mathematical expression is:

$$ H(f)=\frac{1}{Q f + 1}\tag{2}\label{eq:2} $$

Among them, $H(f)$ is the frequency response function, $f$ is the frequency, and $Q$ is the bandwidth.

2) High Pass Filter

High Pass Filter means that signals of all frequencies except the low frequency band are filtered out. This filter can be used to eliminate the weak frequency components of the signal and have a large gain on the entire signal amplitude.

Its mathematical expression is:

$$ H(f)=\frac{s}{Q s + 1}\tag{3}\label{eq:3} $$

Among them, $s=j \omega$ is the virtual conjugate, $\omega$ is the angular frequency.

3) Bandpass filter

Band Pass Filter means that signals within a certain frequency range are retained, while other signals are filtered out. It plays an extremely important role in the field of signal processing.

Its mathematical expression is:

$$ H(f)=\frac{1}{Q f_2 – Q f_1 + 1}\tag{4}\label{eq:4} $$

Among them, $f_1$ and $f_2$ are the two frequency endpoints to be retained.

4) Bandstop filter

Band Stop Filter means that all signals whose frequency range is outside a certain limited interval are filtered out. The basic idea is to prevent signals of specific frequencies from entering by selecting an appropriate resistance value, thereby achieving the purpose of shielding signals of specific frequencies.

Its mathematical expression is:

$$ H(f)=\frac{s^2 + 2bws + w^2}{(w^2-wc)(s^2 + 2bws + w^2)}\tag{5}\label {eq:5} $$

Among them, $s=j \omega$ is the virtual conjugate, $\omega$ is the angular frequency.

5) Butterworth filter

Butterworth Filter is the most commonly used time-frequency filter. Its mathematical expression is as follows:

$$ H(f)=\frac{1}{(1 + \frac{\epsilon}{Q} f)^m}\tag{6}\label{eq:6} $$

Among them, $m$ is a positive integer, $\epsilon$ is a positive number, $f$ is the frequency, and $Q$ is the bandwidth.

6) Chebyshev filter

Chebyshev Type I Filter is a bandpass filter, which is a linear quality filter designed using the “Chebyshev” sinusoid. Its design parameters are all given, so the structure of the filter will change with different parameters without convergence or attenuation.

Its mathematical expression is:

$$ H(f)=\frac{K_1(f)-K_2(f)}{A K_2(f)}=\frac{1}{\Biggl[\frac{W_0}{C} + \frac{1}{wc} + \frac{\omega_c^2}{Q}\Biggr]^2}\tag{7}\label{eq:7} $$

Among them, $K_1(f), K_2(f)$ are the exponential terms of sine and negative half waves, $W_0$ is the stop bandwidth, $C$ is the pass bandwidth, $\omega_c$ is the passband center frequency, $Q $ is the passband attenuation factor.

7) Elliptic filter

Elliptic filter (Elliptic Filter) is a band-pass filter. Its design can ensure that the filter order is controlled within a fixed range and is accurately determined.

Its mathematical expression is:

$$ H(f)=\frac{a_0}{(1 + (\frac{f}{wc})^2)^{(\alpha/2)}}\cos (\theta) \tag{8}\label{eq:8} $$

Among them, $\alpha$ is the exponent.

8) Bessel filter

Bessel filter (Bessel Filter) is a nonlinear filter, which is obtained by interpolating functions through some special eigenvalues. Its filtering performance and accuracy have been greatly improved.

Its mathematical expression is:

$$ H(f)=\frac{J_1 (w_0 f) J_0 (-\pi f r)}{r^2}\tag{9}\label{eq:9} $$

Among them, $J_0,\ J_1$ is the Bessel function, $w_0$ is the damped oscillation frequency, and $r$ is the ratio between the cutoff frequency and the damped oscillation frequency.

5. Mixed Signal Processing

Mixed signal processing usually refers to the combination, combination operation and analysis of multiple signals. By analyzing the frequency response function of each signal and combining the effects of different channels, various mixed signals can be constructed.

1) Spectrum analysis of AM signal

AM (Amplitude Modulation) signal refers to the signal after the carrier and subcarrier are multiplied. The spectrum analysis method is as follows:

$$ S_{\text {AM}}(f)=A S_{\text {signal }}(f) + M S_{\text {carrier }}(f)\tag{10}\ label{eq:10} $$

Among them, $S_{\text {AM}}$ is the power spectral density of the AM signal, $f$ is the frequency; $S_{\text {signal}}$ is the power spectral density of the carrier signal; $S_{\ \text {carrier}}$ is the power spectral density of the subcarrier signal; $A$ and $M$ are the phases of the carrier signal and subcarrier signal respectively.

Through FFT or Fourier transform, the spectrum of the AM signal can be calculated.

2) Joint Spectrum Model

The joint spectrum model refers to mixing the spectrum of two or more signals together to describe the power spectral density of the mixed signal. The mixture model of the spectrum can be expressed as follows:

$$ S_{\text {mixture}}(f)=\sum {i=1}^{k} a{i} S_{\text {signal i }}(f ) + \sum {j=1}^{l} b{j}(f)\cdot e^{\mathrm {j} k\varphi }\tag{11 }\label{eq:11} $$

Among them, $S_{\text {mixture}}$ is the power spectral density of the mixed signal; $a_i$, $b_j$ are the coefficients of two or more signals; $S_{\text {signal i}} $ is the power spectral density of the $i$th signal; $k$ and $l$ are the numbers of the $i$th signal and the $j$th signal; $\varphi$ is the mixed signal phase.

3) Subspace method to estimate signal parameters

The subspace method for estimating signal parameters refers to using the signal matrix and observation vector to estimate the parameters of the signal. Suppose there are $L$ signals and $(n_1, n_2,…, n_L)$ signal observations. Matrix $X=[x_1(1), x_2(1),…, x_L(1); x_1(2), x_2(2),…, x_L(2);… ; x_1(n_1 ), x_2(n_1),…, x_L(n_1)]$ is the signal observation value, and the vector $Y=[y_1; y_2;… ; y_{n_1}]$ is the signal matrix. One way to estimate the signal matrix $A$ and the phase matrix $\Phi$ is to use matrix equations to solve:

$$ \left[ X^\mathrm T X \right] A = X^\mathrm T Y\tag{12}\label{eq:12} $$

$$ \left[ X^\mathrm T X \right] \Phi = X^\mathrm T [e^{j\varphi_1}, e^{j\varphi_2},…, e ^{j\varphi_{n_1}}]\tag{13}\label{eq:13} $$

Among them, $^\mathrm T$ represents transposition.

4) DFT spectrum estimation

DFT (Discrete Fourier Transform) is a method of quickly calculating the discrete Fourier Transform (Discrete Fourier Transform, DFT). DFT spectrum estimation refers to estimating the spectrum of a signal through sample data. Its calculation formula is as follows:

$$ S_{\text {estimated }}(k)=\frac{1}{N}\sum ^{N-1}_{n=0} x[n] e^{-j2\ pi kn / N}\tag{14}\label{eq:14} $$

Among them, $S_{\text {estimated}}$ is the estimated spectrum, $k$ is the frequency coordinate of DFT; $N$ is the sample length, $x[n]$ is the discrete time series of $N$ samples .

5) Signal Mixing Example

Here we show a case of signal mixing processing. First, generate two second-order sinusoidal waveforms as signals:

import numpy as np
import matplotlib.pyplot as plt

Fs = 100 # sampling rate (Hz)
Ts = 1/Fs # sampling interval (seconds)
T = 10 # total time of signal (seconds)

t = np.linspace(0, T, int(T*Fs)) # time vector

f1 = 1 # frequency of first component (Hz)
f2 = 5 # frequency of second component (Hz)

s1 = np.sin(2*np.pi*f1*t) # amplitude modulated signal at freqency f1
s2 = np.sin(2*np.pi*f2*t) # amplitude modulated signal at freqency f2

Next, mix the two signals above:

# create mixture signal by summing the two components and applying an offset to each one
offset=0.5
mixed_signal = s1 + s2 + offset

plt.plot(t, mixed_signal)
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.title('Mixed Signal')
plt.show()

Finally, the original signal is estimated by estimating its spectrum:

# estimate original signals using the DFT method
N = len(mixed_signal)
freq = np.arange(-Fs/2, Fs/2)*float(Fs)/N # frequency axis for plotting purposes
fft_mixed_signal = np.abs(np.fft.fft(mixed_signal))**2/(N/2) # FFT of the mixed signal
fft_s1 = np.abs(np.fft.fft(s1))**2/(N/2) # FFT of the first signal component
fft_s2 = np.abs(np.fft.fft(s2))**2/(N/2) # FFT of the second signal component

# plot estimated signals in the time domain and frequency domain
fig, ax = plt.subplots(nrows=2, figsize=(8,6))
ax[0].set_title('Estimated Signals')
ax[0].plot(t[:int(len(mixed_signal)/2)], fft_mixed_signal[:int(len(mixed_signal)/2)])
ax[0].plot(t[:int(len(s1)/2)], fft_s1[:int(len(s1)/2)])
ax[0].plot(t[:int(len(s2)/2)], fft_s2[:int(len(s2)/2)])
ax[0].legend(['Mixed', 'Component 1', 'Component 2'])
ax[0].set_xlabel('Time')
ax[0].set_ylabel('Power Spectrum Magnitude')

ax[1].plot(freq[:int(N/2)], fft_mixed_signal[:int(N/2)])
ax[1].plot(freq[:int(N/2)], fft_s1[:int(N/2)])
ax[1].plot(freq[:int(N/2)], fft_s2[:int(N/2)])
ax[1].legend(['Mixed', 'Component 1', 'Component 2'])
ax[1].set_xlabel('Frequency (Hz)')
ax[1].set_ylabel('Power Spectrum Magnitude')
plt.show()

The result of the above code is shown below: