-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample3.py
More file actions
190 lines (136 loc) · 4.57 KB
/
Example3.py
File metadata and controls
190 lines (136 loc) · 4.57 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
===========================================================
Module: AM/FM Signal Processing - Local Parameter Estimation
Author: Dominique Fourer
Email: [email protected]
Reference: Dominique Fourer, François Auger, Geoffroy Peeters,
"Local AM/FM parameters estimation: application
to sinusoidal modeling and blind audio source separation,"
IEEE Signal Processing Letters, Vol. 25, Issue 10,
pp. 1600-1604, Oct. 2018, DOI: 10.1109/LSP.2018.2867799
===========================================================
Description:
------------
This Python module implements tools for local AM/FM parameter estimation,
including functions to generate AM/FM signals, compute STFT, and perform
sine parameter reassignment. The implementation is inspired by the
methodology described in the referenced IEEE Signal Processing Letters paper.
Dependencies:
-------------
- numpy
- scipy
- matplotlib
===========================================================
Signal parameter estimation and reconstruction example.
This script demonstrates the complete workflow for testing a sinusoidal
AM–FM parameter estimation algorithm.
Steps:
1. Generate a synthetic signal with known parameters:
- amplitude (a)
- amplitude modulation rate (mu)
- initial phase (phi)
- angular frequency (omega)
- frequency modulation rate (psi)
2. Add white Gaussian noise to the signal in order to obtain a desired
input Signal-to-Noise Ratio (SNR).
3. Estimate the signal parameters from the noisy observation using
a reassignment-based estimation method.
4. Reconstruct the signal from the estimated parameters.
5. Compare the reconstructed signal with the original one using
error metric (RQF) and visualize the results.
The goal is to validate the accuracy of the parameter estimation
algorithm under noisy conditions.
"""
import matplotlib.pyplot as plt
import numpy as np
import reassignment as rs
import my_stft as st
###############################################
# Generate AM-FM signal with known parameters
###############################################
Fs = 22050
N = 1000
t = st.time_axis(N, Fs)
# ----- True parameters -----
a = 1.2
mu = 10
psi = 9000
phi = st.modulo2pi(np.random.rand() * 2*np.pi)
omega = 2*np.pi*440
# signal
x = 2*np.real(a * np.exp(mu*t + 1j*(phi + omega*t + psi*t**2/2)))
#plt.plot(x)
#plt.show()
# add noise
snr_in = 30
s = st.sigmerge(x, np.random.randn(N), snr_in)
###############################################
# Estimation
###############################################
q_method = 3
a_method = 2
k = 2
(a_hat, mu_hat, phi_hat, omega_hat, psi_hat,
delta_t_hat, delta_amp, m_idx, Xw, q) = rs.my_reassignment(
s, Fs, k, q_method, a_method
)
print(m_idx)
print(a_hat)
print(mu_hat)
print(st.modulo2pi(phi_hat))
print(omega_hat)
print(psi_hat)
# exit()
###############################################
# Reconstructed signal
###############################################
x_hat = 2*np.real(a_hat * np.exp(mu_hat*t + 1j*(phi_hat + omega_hat*t + psi_hat*t**2/2)))
###############################################
# Print parameters
###############################################
print("\n===== TRUE PARAMETERS =====")
print(f"a = {a}")
print(f"mu = {mu}")
print(f"phi = {phi}")
print(f"omega = {omega/(2*np.pi):.2f} Hz")
print(f"psi = {psi/(2*np.pi):.2f} Hz/s")
print("\n===== ESTIMATED PARAMETERS =====")
print(f"a_hat = {a_hat}")
print(f"mu_hat = {mu_hat}")
print(f"phi_hat = {phi_hat}")
print(f"omega_hat = {omega_hat/(2*np.pi):.2f} Hz")
print(f"psi_hat = {psi_hat/(2*np.pi):.2f} Hz/s")
print(f"delta_t = {delta_t_hat}")
print(f"delta_amp = {delta_amp}")
rqf_val = st.rqf(x, x_hat)
print("\n===== RQF =====")
print("RQF =", rqf_val)
###############################################
# Plot signals
###############################################
plt.figure(figsize=(10,4))
plt.plot(t[:1000], s[:1000], label="noisy signal")
plt.plot(t[:1000], x_hat[:1000], 'k--', label="estimated signal")
plt.xlabel("time (s)")
plt.title("Signal reconstruction")
plt.legend()
plt.grid()
###############################################
# Spectrogram
###############################################
Sw,z,z = st.my_stft(s, st.hann_window(N), rec=8)
Nh = N//2
plt.figure(figsize=(10,4))
plt.imshow(
20*np.log10(np.abs(Sw[:Nh,:])+1e-12),
aspect='auto',
origin='lower',
cmap='magma'
)
plt.xlabel("frame")
plt.ylabel("frequency bin")
plt.title("Spectrogram of the analyzed signal")
plt.colorbar(label="dB")
plt.show()