0

I am trying to filter for a frequency of e.g. ~3.1Hz in a time-based signal which contains various frequency components (including the ~3.1Hz).

I tried to build a bandpass filter with a lowpass and a highpass filter. After plotting the results the filter does not seem to work regarding the amplitude and phase. Especially at the beginning as you can see in this photo.

filter does not seem to work mainly at the beginning

Therefore I also tried to enhance to signal with itself, then filter the enhanced signal for ~3.1Hz and afterward cut off the enhanced parts.

Where is my mistake?

x= linspace(0,1.6,4096);
funct=5*sin(20*x+0.9); %~3,1 Hz
plot(x,funct); %frequency I want to filter for
hold on;

funct=funct+ 3*sin(210*x); %inject other frequency components
plot(x,funct); %Time based Signal 

%%% Enhence the length of the signal:
R=0.5; % 50% of signal
N=size(funct,2);
NR=round(N*R); % At most 2048 points

    x1(1,:)=2*funct(1,1)-flipud(funct(1, 2:NR+1));  % maintain continuity in level and slope
    x2(1,:)=2*funct(1, end)-flipud(funct(1, end-NR:end-1));

x_appd=[x1, funct ,x2];
%%%

%%% Do filtering
[b,a]=butter(3, 1/1000, "high");
x1_filt=filter(b,a,x_appd);

[c,d]=butter(3, 5/1000, "low");
x2_filt=filter(c,d,x1_filt);
%%%

x_ergb=x2_filt(:,NR+1:end-NR); %Cut of the enhenced parts of the signal

plot(x,x_ergb); %Plot the filtered function
hold off;

Thanks for your help!

I tried different filter settings and filters but nothing seems to work. Is there a solution to exactly filter for the desired frequency?

4
  • 2
    MATLAB's butter function already allows you to use a bandpass filter. If you use the [b,a] = butter(n,Wn) formulation with Wn containing two elements then bandpass is the default. Commented Jun 7, 2024 at 12:33
  • Also too, the input argument n is an integer defining the order of your filter, and Wn is a scalar (or a two element array for what you want to do), containing the cut-off frequencies in Hertz. So instead of using 1/1000 and 5/1000 you should directly use Wn = [1 5] Commented Jun 7, 2024 at 12:36
  • 1
    @BillBokeey its not the cut-off frequency in Hz, its the normalized cutoff frequency, ie Wn = [Flow Fhigh]/(Fs/2) Commented Jun 7, 2024 at 12:52
  • 1
    If you use filter you introduce a phase-shift. To filter without introducing a phase shift, use filtfilt, it applies the filter twice in forward and backward direction resulting in 0 phase shift. Commented Jun 7, 2024 at 12:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.