how to filter a signal ?
2 views (last 30 days)
Show older comments
hello; The filtering of a complete signal x using the filter function in matlab (filter(b,1,x)) will give the same results if done block by block this is an example
[y,Fs] = audioread ('Rock.wav');
t_bit_in_samples =6125;
k1 = [zeros(d1,1);-1;1]*0.3;
k1= [1;k1];
out1 = filter(k1,1,y);
for(index=1:t_bit_in_samples:legth(y))
out(index:(index+t_bit_in_samples-1))=filter (k1,1,s(index:(index+t_bit_in_samples-1)));
end;
so my question is how to obtain the same result of out1 using block by block thank you for your answer.
0 Comments
Answers (1)
Jan
on 30 Aug 2017
Edited: Jan
on 30 Aug 2017
Please care for posting valid code. No parentheses around the argument of for and "legth" is assumed to be "length" and "s" to be "y". I guess that d1 is 7. I stop the loop at length(y) - tb + 1 instead of length(y). It is tedious to fix such details.
[y, Fs] = audioread('Rock.wav');
tb = 6125;
k1 = [1; zeros(d1, 1); -0.3; 0.3];
% Filter in one step
out1 = filter(k1, 1, y);
% Filter blockwise:
z = zeros(1, d1 + 2);
out = zeros(size(y));
for k = 1:tb:length(y) - tb + 1
kf = k + tb - 1;
[out(k:kf), z] = filter (k1, 1, y(k:kf), z);
end
% Last chunk:
out(kf + 1:end) = filter (k1, 1, y(kf+1:end), z);
For filtering in blocks, you have to keep the current state of the filter z and provide it for the next block.
2 Comments
Jan
on 30 Aug 2017
Edited: Jan
on 30 Aug 2017
What is the problem exactly? Filtering in chunks is not faster, but slower than doing it at once. It would be useful if you explain the problem in detail.
Do you want to accelerate the filtering? This should be easy with a hard coded implementation, because most elements of the filter parameter b are zeros.
Is d1 fixed? Then I could provide a C-Mex function. Do you have a C compiler installed?
Did you see that it took me some time to fix the typos in your code? It is a good idea to reduce the work required for answer as much as possible.
See Also
Categories
Find more on Signal Processing Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!