Ways to Apply filter for Signal
23 views (last 30 days)
Show older comments
I have a matrix of signals, with each row representing a signal, and a corresponding matrix of filters, where each row represents the filter for a specific signal. How can I efficiently apply the convolution of each signal with its corresponding filter?
0 Comments
Answers (2)
Star Strider
on 27 Nov 2024 at 18:14
A loop may be appropriiate. Put the fiilters in a cell array, and then call each filter with its appropriate signal.
Example —
Fs = 1000;
Fn = Fs/2;
Wp = [1 100]/Fn; % Passband Frequency (Normalised)
Ws = [0.5 105]/Fn; % Stopband Frequency (Normalised)
Rp = 1; % Passband Ripple
Rs = 60; % Passband Ripple (Attenuation)
[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Elliptic Filter Design: Zero-Pole-Gain
[sos{1},g{1}] = zp2sos(z,p,k); % Second-Order Section For Stability
Wp = [100 200]/Fn; % Passband Frequency (Normalised)
Ws = [95 205]/Fn; % Stopband Frequency (Normalised)[n,Wp] = ellipord(Wp,Ws,Rp,Rs); % Elliptic Order Calculation
[z,p,k] = ellip(n,Rp,Rs,Wp); % Elliptic Filter Design: Zero-Pole-Gain
[sos{2},g{2}] = zp2sos(z,p,k); % Second-Order Section For Stability
t = linspace(0, 5, 10000);
s = randn(2, 10000); % Signal Matrix
for k = 1:size(s,1)
out{k} = filtfilt(sos{k},g{k},s(k,:));
end
figure
tiledlayout(size(s,1), 1)
for k = 1:size(s,1)
nexttile
plot(t, out{k})
grid
title("Signal "+k)
end
.
0 Comments
Gayatri
on 27 Nov 2024 at 18:41
Hi Nikhil,
If you have a matrix of signals where each row represents a distinct signal and another matrix of filters with each row corresponding to a filter, you can apply the associated filters to the signals simultaneously.
- Convert the matrices into cell arrays and leverage the 'cellfun' function to apply convolution row by row as follows:
A = [1 0 1; 1 0 1]; % Matrix A: Contains signals as rows.
B = [2 7; 2 7]; % Matrix B: Contains filters as rows.
- Transform each matrix into a cell array, where each cell holds a single row of the original matrix. This enables efficient row-wise operations:
cellA = num2cell(A, 2);
cellB = num2cell(B, 2);
- Use 'cellfun' to perform convolution between each signal and its corresponding filter:
result = cellfun(@(x, y) conv(x, y), cellA, cellB, 'UniformOutput', false);
Here, 'cellfun' applies the convolution function to each pair of corresponding rows from the two cell arrays (cellA and cellB). The 'UniformOutput', false option allows the output to be stored as a cell array because convolution can produce results of varying lengths.
For additional information on the 'cellfun' function, refer to the MATLAB documentation: https://www.mathworks.com/help/releases/R2022a/matlab/ref/cellfun.html
I hope it helps!
0 Comments
See Also
Categories
Find more on Digital Filtering in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!