I've been trying to implement the MATLAB code of Prof.Selesnick's DWT implementation in Python for learning purposes.
function [lo, hi] = afb(x, af)
% [lo, hi] = afb(x, af)
%
% Analysis filter bank
% x -- N-point vector (N even); the resolution should be 2x filter length
%
% af -- analysis filters
% af(:, 1): lowpass filter (even length)
% af(:, 2): highpass filter (even length)
%
% lo: Low frequency
% hi: High frequency
%
N = length(x);
L = length(af)/2;
x = cshift(x,-L);
% lowpass filter
lo = upfirdn(x, af(:,1), 1, 2);
lo(1:L) = lo(N/2+[1:L]) + lo(1:L);
lo = lo(1:N/2);
% highpass filter
hi = upfirdn(x, af(:,2), 1, 2);
hi(1:L) = hi(N/2+[1:L]) + hi(1:L);
hi = hi(1:N/2);
I'm specifically stuck at lo(1:L) = lo(N/2+[1:L]) + lo(1:L);
I attempted lo[np.arange(0,L)]=lo[N // 2 + np.concatenate([np.arange(0,L)])) + lo[np.arange(0,L)]
but it seems not to be working. Will appreciate any help.
I have an input signal x with a size of 10,000, when I execute the code it stops right at that specific line and says index 5001 is out of bounds for axis 0 with size 5001. I seem to be out of bounds.
The numpy library already have a bunch of itens that helps us with the slices.
When you need to take a slice of a numpy array, for example
a = np.array([1,2,3,4,5,6,7,8,9,10]), you can slice witha[:n]andnwill be the last element in the sequence. Ifn = 2thena[:2] = [1,2]. Try this and you will write a clean code.For your error of "out of bounds" i recommend you to check the indices and remember that Python count starts at 0.
For example (using our "a" array defined above):
Note that the last element is at indice 9. I believe this is your error, you must use
foo[5000]instead offoo[5001].I hope i helped you a little!