MATLAB Answers

Amplitude of signal not matching up after filtering when calculating it using different methods.

2 views (last 30 days)
Giggs B.
Giggs B. on 5 Aug 2021
Commented: Giggs B. on 5 Aug 2021
I am running a code where I am generating a signal. I am doing processing on the signal using 2 ways mentioned below:
  1. I am filtering the signal and then calculating the amplitude of the signal (Sum1).
  2. I am breaking the signal into chunks of 2000 data samples, filtering each chunk seperately and calculating amplitude of each chunk of data and then summing up the amplitude of all the chunks (Sum2).
I have tried this code with both [b,a] method and [z,p,k] method. The problem is I am not getting both the sums matched up. There's always a difference between them, however I want to mention that when using [z,p,k] method, the difference is very low (<2) as opposed to (~30) when using [b,a] method. I am not sure why that is happening. I am not using filtfilt since I have to implement my code in hardware and hence my hardware won't be doing processing the way as filtfilt does. One reason I can think of is maybe related to phase shift? Can someone put their thoughts into this? Thank you!
[b,a]=ellip(4, 1, 40, 100/(fs/2),'high'); %Elliptical filter
% [z,p,k] = ellip(4,1,40,2000/(fs/2),'high'); %z,p,k method
% [sos,g] = zp2sos(z,p,k);
y = rand (10000,1);
Sum1 = sum(abs(y_h2)); %amplitude of whole signal together
s2_sections = buffer(y,2000);
[rows, columns] = size(s2_sections);
s2_filtered = zeros(rows,columns);
for col = 1:columns
thiscolumn = s2_sections(:,col);
s2_filtered(:,col) = filter(b,a,thiscolumn);
[rows2,columns2] = size(s2_filtered);
y2_amplitude = zeros(1,columns2);
for col2=1:columns2
thiscolumn2 = s2_filtered(:,col2);
Sum2 = sum(abs(y2_amplitude)); %amplitude of signal when processing done on chunks of data

Accepted Answer

Walter Roberson
Walter Roberson on 5 Aug 2021
Every separate filter() call initializes the filter state to zeroes before processing the figure. Every window that you process this way effectively introduces transcients.
You can avoid this by outputing the filter state and using that in the next filter() call:
for col = 1:columns
thiscolumn = s2_sections(:,col);
if col == 1
[s2_filtered(:,col), zi] = filter(b, a, thiscolumn);
[s2_filtered(:,col), zi] = filter(b, a, thiscolumn, zi);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by