Spaces:
Running
Running
Commit
·
e3bff8a
1
Parent(s):
74e0bd0
fix: divide by zero in log10
Browse files- plot_utils.py +41 -0
plot_utils.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
+
from scipy.signal import freqz
|
4 |
+
from typing import Iterable
|
5 |
+
|
6 |
+
from modules import fx
|
7 |
+
from modules.functional import (
|
8 |
+
highpass_biquad_coef,
|
9 |
+
lowpass_biquad_coef,
|
10 |
+
highshelf_biquad_coef,
|
11 |
+
lowshelf_biquad_coef,
|
12 |
+
equalizer_biquad_coef,
|
13 |
+
)
|
14 |
+
|
15 |
+
|
16 |
+
def get_log_mags_from_eq(eq: Iterable, worN=1024, sr=44100):
|
17 |
+
get_ba = lambda xs: torch.cat([x.view(1) for x in xs]).view(2, 3)
|
18 |
+
|
19 |
+
def f(biquad):
|
20 |
+
params = biquad.params
|
21 |
+
match type(biquad):
|
22 |
+
case fx.HighPass:
|
23 |
+
coeffs = highpass_biquad_coef(sr, params.freq, params.Q)
|
24 |
+
case fx.LowPass:
|
25 |
+
coeffs = lowpass_biquad_coef(sr, params.freq, params.Q)
|
26 |
+
case fx.HighShelf:
|
27 |
+
coeffs = highshelf_biquad_coef(sr, params.freq, params.gain, biquad.Q)
|
28 |
+
case fx.LowShelf:
|
29 |
+
coeffs = lowshelf_biquad_coef(sr, params.freq, params.gain, biquad.Q)
|
30 |
+
case fx.Peak:
|
31 |
+
coeffs = equalizer_biquad_coef(sr, params.freq, params.gain, params.Q)
|
32 |
+
case _:
|
33 |
+
raise ValueError(biquad)
|
34 |
+
|
35 |
+
b, a = get_ba(coeffs)
|
36 |
+
w, h = freqz(b.numpy(), a.numpy(), worN, fs=sr)
|
37 |
+
log_h = 20 * np.log10(np.abs(h) + 1e-10)
|
38 |
+
return w, log_h
|
39 |
+
|
40 |
+
log_mags = list(map(f, eq))
|
41 |
+
return log_mags[0][0], [x for _, x in log_mags]
|