-
Notifications
You must be signed in to change notification settings - Fork 1
/
phasevocoder.py
144 lines (100 loc) · 4.23 KB
/
phasevocoder.py
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
import numpy as np
import scipy.signal
# TODO: Move peak detection flags to command-line args
MATCH_PEAKS = True
PEAK_MAX_DIST = 16
PEAK_THRESH = 0.01
class PhaseVocoder:
def __init__(self, samplerate, blocksize):
self.samplerate = samplerate
self.blocksize = blocksize
self.fft_size = (blocksize // 2) + 1
self.last_phase = np.zeros(self.fft_size)
self.last_phase_out = np.zeros(self.fft_size)
self.window = np.hanning(blocksize)
self.freq = np.fft.rfftfreq(blocksize, 1 / samplerate)
def analyze(self, block, advance):
in_block = block * self.window # np.fft.fftshift()
fft = np.fft.rfft(in_block)
magnitude = np.abs(fft)
phase = np.angle(fft)
dt = advance / self.samplerate
min_f = self.est_freqs_div(phase, dt)
self.last_phase = phase
return magnitude, phase, min_f
def est_freqs_div(self, phase, dt):
# TODO: This runs into problems at first bin, investigate
freq_base = (phase - self.last_phase) / (2 * np.pi * dt)
n = np.maximum(np.round((self.freq - freq_base) * dt), 0)
min_f = freq_base + (n / dt)
return min_f
def constrain_phase(self, phase):
return ((phase + np.pi) % (np.pi * 2)) - np.pi
def synthesize(self, magnitude, frequency, advance):
dt = advance / self.samplerate
out_phase = self.last_phase_out + 2 * np.pi * frequency * dt
out_phase = self.constrain_phase(out_phase)
self.last_phase_out = out_phase
fft = magnitude * np.exp(1j * out_phase)
out_block = np.fft.irfft(fft) * self.window
return out_block
class PeakPhaseVocoder(PhaseVocoder):
def __init__(self, samplerate, blocksize):
super().__init__(samplerate, blocksize)
self.last_peaks = []
def compare(self, a, b):
return np.greater(a, b + PEAK_THRESH)
def analyze(self, block, advance):
magnitude, phase, freq = super().analyze(block, advance)
# peak_pos, prop = scipy.signal.find_peaks(magnitude, width=9)
peak_pos = scipy.signal.argrelextrema(magnitude, self.compare, order=4)[0]
# print(peak_pos)
peak_start = []
peak_end = []
for i, peak in enumerate(peak_pos):
start = 0
if i != 0:
start = np.argmin(magnitude[peak_pos[i - 1] : peak])
# start = (peak_pos[i-1] + peak) // 2
peak_end.append(start)
peak_start.append(start)
peak_end.append(magnitude.size)
# plt.plot(magnitude)
# plt.scatter(peak_pos, magnitude[peak_pos])
# plt.show()
return magnitude, phase, freq, list(zip(peak_pos, peak_start, peak_end))
def synthesize(self, magnitude, phase, frequency, peaks, in_adv, out_adv):
dt = out_adv / self.samplerate
out_phase = np.zeros(phase.size)
alpha = out_adv / in_adv
# TODO: Also try beta = 1
beta = alpha
# print("last",self.last_peaks)
for peak, peak_start, peak_end in peaks:
old_peak = peak
if MATCH_PEAKS:
min_dist = None
for last_peak, lp_start, lp_end in self.last_peaks:
dist = abs(peak - last_peak)
# print(dist)
if (min_dist == None or dist < min_dist) and dist <= PEAK_MAX_DIST:
min_dist = dist
old_peak = last_peak
peak_phase = (
self.last_phase_out[old_peak] + 2 * np.pi * frequency[peak] * dt
)
# force-lock partial phases to the peak phase
out_phase[peak_start:peak] = peak_phase + beta * (
phase[peak_start:peak] - phase[peak]
)
out_phase[peak + 1 : peak_end] = peak_phase + beta * (
phase[peak + 1 : peak_end] - phase[peak]
)
out_phase[peak] = peak_phase
out_phase = self.constrain_phase(out_phase)
self.last_phase_out = out_phase
self.last_peaks = peaks
# self.window_out(magnitude, out_phase)
fft = magnitude * np.exp(1j * out_phase)
out_block = np.fft.irfft(fft) * self.window
return out_block