-
Notifications
You must be signed in to change notification settings - Fork 20
generate coefficients for WFS FIR prefilters #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,3 +197,65 @@ def apply_delays(signal, delays, fs=None): | |
for channel, cdelay in enumerate(delays_samples): | ||
out[cdelay:cdelay + len(signal), channel] = signal | ||
return out, offset_samples / fs | ||
|
||
|
||
def wfs_prefilter(dim='2.5D', N=128, fl=50, fu=2000, fs=None, c=None): | ||
"""Get pre-equalization filter for WFS. | ||
|
||
Rising slope with 3dB/oct ('2.5D') or 6dB/oct ('2D' and '3D'). | ||
Constant magnitude below fl and above fu. | ||
Type 1 linear phase FIR filter of order N. | ||
Simple design via "frequency sampling method". | ||
|
||
Parameters | ||
---------- | ||
N : int, optional | ||
Filter order, shall be even. For odd N, N+1 is used. | ||
dim : str, optional | ||
Dimensionality, must be '2D', '2.5D' or '3D'. | ||
fl : int, optional | ||
Lower corner frequency in Hertz. | ||
fu : int, optional | ||
Upper corner frequency in Hertz. | ||
(Should be around spatial aliasing limit.) | ||
fs : int, optional | ||
Sampling frequency in Hertz. | ||
c : float, optional | ||
Speed of sound. | ||
|
||
Returns | ||
------- | ||
h : (N+1,) numpy.ndarray | ||
Filter taps. | ||
delay : float | ||
Pre-delay in seconds. | ||
|
||
""" | ||
N = 2*(int(N + 1)//2) # for odd N, use N+1 instead | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is only use once below, where it is divided by two and one is added. That's a lot of dividing by 2 and adding one, isn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's ugly, but intentional. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't talking about that. But I admit that I wasn't quite clear ... I was talking about this: bins = int(2*(int(N + 1)//2)/2 + 1) This makes me dizzy. Is this really the canonical way to write this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got rid of it by raising an error for odd |
||
if fs is None: | ||
fs = defs.fs | ||
if c is None: | ||
c = defs.c | ||
|
||
numbins = int(N/2 + 1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't like "num" prefixes. What about just calling it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, I understand that If you want to make it abundantly clear that it is a quantity rather than a list of things, you should call it something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You are probably right. |
||
delta_f = fs / (2*numbins - 1) | ||
f = np.arange(numbins) * delta_f | ||
if dim == '2D' or dim == '3D': | ||
alpha = 1 | ||
elif dim == '2.5D': | ||
alpha = 0.5 | ||
|
||
desired = np.power(2 * np.pi * f / c, alpha) | ||
low_shelf = np.power(2 * np.pi * fl / c, alpha) | ||
high_shelf = np.power(2 * np.pi * fu / c, alpha) | ||
|
||
l_index = int(np.ceil(fl / delta_f)) | ||
u_index = int(min(np.ceil(fu / delta_f), numbins - 1)) | ||
desired[:l_index] = low_shelf | ||
desired[u_index:] = min(high_shelf, desired[u_index]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can Also, do you really need to calculate both Couldn't you just clip Or, alternatively, use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! It is a lot cleaner using |
||
|
||
h = np.fft.ifft(np.concatenate((desired, desired[-1:0:-1]))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you use irfft() here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done. |
||
h = np.roll(np.real(h), numbins - 1) | ||
h = h / np.sqrt(np.sum(abs(h)**2)) # normalize energy | ||
delay = (numbins - 1) / fs | ||
return h, delay |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why don't you just raise a
ValueError
if the user provides wrong input?