Ways to Apply filter for Signal

7 Ansichten (letzte 30 Tage)
Nikhil
Nikhil am 27 Nov. 2024
Beantwortet: Gayatri am 27 Nov. 2024
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?

Antworten (2)

Star Strider
Star Strider am 27 Nov. 2024
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
.

Gayatri
Gayatri am 27 Nov. 2024
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!

Kategorien

Mehr zu Data Type Identification finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by