-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHisMethod-all
More file actions
113 lines (93 loc) · 3.69 KB
/
HisMethod-all
File metadata and controls
113 lines (93 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Structured Jupyter notebook that incorporates the steps for Wavelet Transform, Short-Time Fourier Transform (STFT), and Parametric Methods, along with the preprocessing techniques.
```python
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import pywt
from scipy.signal import stft, detrend, get_window
from statsmodels.tsa.ar_model import AutoReg
# Step 1: Simulate a very low-frequency signal
fs = 100 # Sampling frequency (Hz)
t = np.linspace(0, 1, fs) # Time vector for 1 second
f_low = 0.1 # Low frequency (Hz)
signal = np.sin(2 * np.pi * f_low * t)
# Plot the original signal
plt.figure(figsize=(12, 4))
plt.plot(t, signal, label='Original Signal')
plt.title('Original Signal')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
# Step 2: Preprocessing the Signal
# (a) Detrending
detrended_signal = detrend(signal)
# (b) Windowing
window = get_window('hamming', fs)
windowed_signal = detrended_signal * window
# (c) Downsampling
downsampling_factor = 2
downsampled_signal = signal[::downsampling_factor]
downsampled_t = t[::downsampling_factor]
# Plot the preprocessed signal
plt.figure(figsize=(12, 4))
plt.plot(t, signal, label='Original Signal')
plt.plot(t, detrended_signal, label='Detrended Signal')
plt.plot(t, windowed_signal, label='Windowed Signal')
plt.plot(downsampled_t, downsampled_signal, label='Downsampled Signal', linestyle='--')
plt.title('Preprocessed Signals')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
# Step 3: Wavelet Transform
# Continuous Wavelet Transform (CWT)
widths = np.arange(1, 128)
cwt_matrix, freqs = pywt.cwt(signal, widths, 'mexh', 1.0 / fs)
# Plot the CWT result
plt.figure(figsize=(12, 6))
plt.imshow(np.abs(cwt_matrix), extent=[0, 1, 1, 128], cmap='PRGn', aspect='auto', vmax=abs(cwt_matrix).max(), vmin=-abs(cwt_matrix).max())
plt.title('Wavelet Transform (CWT)')
plt.xlabel('Time (s)')
plt.ylabel('Scale')
plt.colorbar(label='Magnitude')
plt.show()
# Step 4: Short-Time Fourier Transform (STFT)
# Compute STFT
f, t_stft, Zxx = stft(signal, fs, nperseg=20)
# Plot STFT result
plt.figure(figsize=(12, 6))
plt.pcolormesh(t_stft, f, np.abs(Zxx), shading='gouraud')
plt.title('Short-Time Fourier Transform (STFT)')
plt.xlabel('Time (s)')
plt.ylabel('Frequency (Hz)')
plt.colorbar(label='Magnitude')
plt.show()
# Step 5: Parametric Methods (Autoregressive Model)
# Fit AR model to the detrended signal
model = AutoReg(detrended_signal, lags=5)
model_fit = model.fit()
ar_psd = model_fit.predict(start=0, end=len(detrended_signal)-1)
# Plot AR model results
plt.figure(figsize=(12, 4))
plt.plot(t, detrended_signal, label='Detrended Signal')
plt.plot(t, ar_psd, label='AR Model PSD', linestyle='--')
plt.title('Autoregressive Model PSD')
plt.xlabel('Time (s)')
plt.ylabel('Amplitude')
plt.legend()
plt.show()
```
### Explanation of the Notebook:
1. **Signal Simulation**: Creates a very low-frequency sinusoidal signal.
2. **Preprocessing the Signal**:
- **Detrending**: Removes trends in the data.
- **Windowing**: Applies a Hamming window to the signal.
- **Downsampling**: Reduces the sampling rate to focus on low-frequency components.
3. **Wavelet Transform**:
- Uses the Continuous Wavelet Transform (CWT) to analyze the signal.
4. **Short-Time Fourier Transform (STFT)**:
- Computes the STFT to get the time-frequency representation.
5. **Parametric Methods (Autoregressive Model)**:
- Fits an Autoregressive (AR) model to the signal and estimates the Power Spectral Density (PSD).
This notebook provides a comprehensive analysis of very low-frequency signals using various signal processing techniques and ensures the signal is preprocessed appropriately to enhance the quality of the spectral analysis.