-1

I'm on it for a few hours trying to tweak it in all sort of ways but the output comes out scrambled and I can't understand why.

I am trying to implement an HPF with a stopband frequency of 500Hz and passband frequency of 600Hz. This is what I've done so far:

M=131072;                    %the number of samples
Wp=1200*pi;                  %passband cutoff frequency
m=0:M/2                      %the sampling points
q=length(m);
Wm=2*pi*m./(M+1)             %stopband cutoff frequency
mtr=ceil(Wp/(2*pi))          %round to positive part,i.e.ceil(3.5)=4;ceil(-3.2)=-3;
Ad=(Wm>=(Wp/(M+1)))        
Ad(mtr)=0.38
Hd=Ad.*exp(-j*0.5*M*Wm)      %frequency domain sampling vector H(k)
Hd=[Hd conj(fliplr(Hd(2:M/2+1)))]
h=real(ifft(Hd))  
w=linspace(0,q,1966140)      %linspace(x1,x2,n) generates n points. The spacing between the 
                             %points is (x2-x1)/(n-1).

H=freqz(h,(1),w); %the amplitude -frequency characteristic diagram of the filter figure(1) plot(w,20*log10(abs(H))) %parameters are respectively the normalized frequency and %amplitude xlabel('the normailzed frequency');ylabel('gian/dB'); title('Gain response - HighPass Filter'); axis([0 2000 -50 2]);

the output of:

Ad=(Wm>=(Wp/(M+1)))

gives all 1's after cell 601 in the array (601 included).

after:

Ad(mtr)=0.38

I get all 1's after cell 600 in the array (600 included).

I think everything is ok until:

Hd=[Hd conj(fliplr(Hd(2:M/2+1)))]

and maybe something gets scrambled with something here:

w=linspace(0,q,1966140)
H=freqz(h,(1),w);            
figure(4)
plot(w,20*log10(abs(H))) 

the output is: enter image description here

bitmetvt
  • 1
  • 1
  • Your code doesn't run since the variable q is undefined. It's also a lot easier to run if you terminate your statements with a semicolon – Hilmar Dec 22 '21 at 22:18
  • In my code I've defined q to be length(m), for m=0:M/2 . Didn't add it to the script here while editing. – bitmetvt Dec 26 '21 at 15:15
  • furthermore, if you run a live script, not including a semicolon for commands you want to examine the outputs of, will allow you to see it's output below the line while running the live script.. that is why I don't have semicolons for some of the lines here – bitmetvt Dec 26 '21 at 15:17
  • Can you guide me as to how can I change the output frequency band to be 600Hz and onwords? – bitmetvt Dec 26 '21 at 15:22

1 Answers1

0

You have no concept of a sample rate in your code. Everything in digital signal processing is relative to the sample rate. You either need to work in normalized frequency (from $-\pi$ to $\pi$) or absolute frequency (from $-f_s/2$ to $f_s/2$). Your frequency axis makes no sense because it contains thousands of periods and that's why are seeing this "modulation".

If you want something to happen at 600Hz you need to pick a sample rate.

In general, zeroing out FFT bins is not a great way to implement high or lowpass filters. See for example Why is it a bad idea to filter by zeroing out FFT bins?

Hilmar
  • 44,604
  • 1
  • 32
  • 63