directory

Data is read

Fourier transform

The short time Fourier transform

Continuous wavelet transform

Discrete wavelet transform

Data reading First we have such a signal, the following is the data format, namely a column of a number:

We only need the first 500 numbers after reading:

`a = []
b = []
with open("ECG1_1.txt") as file_obj:
     for content in file_obj:
         a.append(int(content))
for i in range(500):
    b.append(a[i])
plt.plot(b)
plt.show()`
Copy the code

Fourier transform ‘fft_b’ = FFT (b)

Angle (fft_b) # plt.figure() plt.plot(abs_b) plt.figure() plt.plot(angle_b) plt.show()`Copy the code

The figure above is frequency amplitude, and the figure below is frequency amplitude Angle:

The short time Fourier transform

Fs: sampling frequency of time series, nperseg: length of each segment, noverlap: number of overlapping segments. Noverlap =nperseg/2 f, t, Nd = signal.stft(b,fs = 1.0,window ='hann',nperseg = 150, Noverlap = 50) plt.pcolormesh(t, f, Np.abs (nd), vmin = 0, vmax = 4) plt.title('STFT') plt.ylabel('frequency') plt.xlabel('time') plt.show()`Copy the code

The following information is displayed:

But this is really hard to analyze, so let’s try a simpler function:

'aa = [] for I in range(200): aa. Append (np.sin(0.3* NP. PI * I)) for I in range(200): Append (np.sin(0.13*np. PI * I)) for I in range(200): Aa.append (np.sin(0.05* Np.pi * I)) plt.plot(aa) plt.show() # fs: sampling frequency of time series, nperseg: length of each segment noverlap: number of overlaps between segments Noverlap =nperseg/2 f, t, Nd = signal.stft(aa,fs = 1.0,window ='hann',nperseg = 150, Noverlap = 50) plt.pcolormesh(t, f, Np.abs (nd), vmin = 0, vmax = 4) plt.title('STFT') plt.ylabel('frequency') plt.xlabel('time') plt.show()`Copy the code

Is shown as:

The frequency display is also very normal: high at the beginning, then lower in the second band, lowest in the third band, while there are two bands of frequency in the overlapping region:

If the Settings window does not overlap, it will be displayed as:

Continuous wavelet transform

'sampling_rate = 1024 WAVename = 'cgau8' Totalscal = 256 # central frequency fc = Pywt. central_frequency(WAVename) # Calculate the wavelet scale corresponding to the frequency cparam = 2 * fc * totalscal scales = cparam / np.arange(totalscal, 1, -1) [cwtmatr, frequencies] = pywt.cwt(aa, scales, Figure (figsize=(8, 4)) plt.subplot(211) t = np.arange(0, 600, 1.0) plt.plot(t, figsize=(8, 4) plt.subplot(211) t = np.arange(0, 600, 1.0) plt.plot(t, 1) aa) plt.xlabel(u"time(s)") plt.subplot(212) plt.contourf(t, frequencies, Abs (cwtmatr) plt.yLabel (u"freq(Hz)") plt.xLabel (u"time(s)") plt.subplots_adjust(hspace=0.4) plt.show() 'Copy the code

Modify the original signal to middle high frequency and low frequency on both sides, and get the result:

Discrete wavelet transform

`wavename = 'db5' cA, cD = pywt.dwt(aa, wavename) ya = pywt.idwt(cA, None, wavename,'smooth') # approximated component yd = pywt.idwt(None, cD, # detailed component x = range(len(aa)) plt.figure(figsize=(12,9)) plt.subplot(311) plt.plot(x, 'smooth') # detailed component x = range(len(aa)) plt.figure(figsize=(12,9)) plt.subplot(311) plt.plot(x, 'smooth') aa) plt.title('original signal') plt.subplot(312) plt.plot(x, ya) plt.title('approximated component') plt.subplot(313) plt.plot(x, yd) plt.title('detailed component') plt.tight_layout() plt.show()`Copy the code

Discrete wavelet transform DWT:

The “continuity” of CWT, and how it differs from the discrete wavelet transform, is the scale and location set in which it operates. Unlike discrete wavelet transform, CWT can operate at every scale, from the scale of the original signal up to some maximum scale. (In the case of computers, of course, it also extracts a certain number of discrete scales.)

Moreover, CWT is also continuous in terms of shifts, that is, shifts smoothly over the entire domain of the analysis function during computation (also, in the case of computers, shifts discretely according to time resolution).

The above function divides the signal into low frequency approximation and high frequency detail: