0
$\begingroup$

In an attempt to gain a better understanding of DSP, I want to create a very simple (1D) lowpass, or I think more correctly "stop-band" or "band-reject" filter, to filter out a single frequency, e.g., f2 in my code example below.

The idea is to build in in the frequency domain, by putting all ones values in every component of the filter's magnitude part EXCEPT of course in the position corresponding to f2. What i don't understand, is what phases am I would be supposed to put in the corresponding LP phase vector? (I think this is related to my problems of understanding phases in general)

For and ideal lowpass filter (brick-wall) I know the corresponding impulse response would be a sinc in the time domain. But e.g. if I built the magnitude part of the brickwall "manually" ones around 0 frequency and 0s for higher freq than the cutoff freq, I would have no idea what to put for the phase ?

My code:

clc;clear all;

fs=1000;
t=0:(1/fs):1-(1/fs);
f1=5;
f2=27;
A1=13;
A2=3;
foffset=3;
posFreqPos=f2+1;
negFreqPos=fs-f2+1;

s=A1*cos(2*pi*f1*t)+A2*cos(2*pi*f2*t);

S=fft(s);
Smag=abs(S);
Sphase=angle(S);

LP_mag=ones(size(S));
LP_mag(posFreqPos)=0;%filter out high freq f2
LP_mag(negFreqPos)=0;

%%%%%%%%%%%%%%%%%%%
% LPphase= ???
%%%%%%%%%%%%%%%%%%%

%the filter built from mag and phase
LPf=LP_mag.*exp(-i.*LPphase);

figure;
plot(LPf)

figure;
plot(s);
figure;
plot(Smag);

I have modified the code here, the first version didn't make sense sorry. It's really a question about the phase.

The idea is to build a filter such as: enter image description here

Following the convolution theorem which states that a point-wise multiplication in the frequency domain corresponds to a convolution in the time domain. Of course one simple solution which "roughly" works is to just set to zero the frequencies I want to remove (following the same idea as shown on the image ) in the magnitude image of the FFT of my image, the rebuild the image with

myImage_filtered = real(ifft(myImageMag_filtered.*exp(-i.*myImagephase))); %real(.) to avoid rounding error causing imaginary part to be non-zero

(image source:http://www.robots.ox.ac.uk/~az/lectures/ia/lect2.pdf )

$\endgroup$
  • 3
    $\begingroup$ That's a bad idea and won't work. See dsp.stackexchange.com/questions/6220/… $\endgroup$ – Hilmar Jul 30 '19 at 18:16
  • 3
    $\begingroup$ Possible duplicate of Why is it a bad idea to filter by zeroing out FFT bins? $\endgroup$ – Marcus Müller Jul 30 '19 at 21:40
  • $\begingroup$ @Hilmar yes sorry, I modified the code, the previous version didn't make much sense. I do this for understanding though, not for a "real" application. I would like to know which phase to put in the " %%% box" $\endgroup$ – Machupicchu Jul 31 '19 at 7:59
  • $\begingroup$ @MarcusMüller I don't think so, since I am mostly interested about the phase, as I have said. $\endgroup$ – Machupicchu Jul 31 '19 at 8:05
  • $\begingroup$ Sorry, your code is not doing anything particularly useful so the choice of phase will make no real difference to the outcome. You are asking the wrong question. Consider asking "what's the best way to remove a single frequency from a signal" and you will get answers that look very different from what you have. Filtering in the frequency domain is rather complicated and you need a good understanding of the math behind it before you can write code that works $\endgroup$ – Hilmar Jul 31 '19 at 9:33
1
$\begingroup$

Typically if you're designing to an amplitude response, you set the phase at zero. In processing time-domain signals, this is to keep the group delay of the filter constant. In processing images, this will keep the filter effects centered, rather than displacing different spectral components of the image with respect to one another.

At least in time-domain processing there can be good reasons not to do this. I can't think of any reason you would want a non-constant delay filter in image processing.

Keeping the phase at zero makes a constant-delay filter. If you're doing your filtering after the fact, then you can just delay everything else by the same amount so that it appears that you're using a zero-delay noncausal filter.

However, if you're responding to the filter output in the real world (usually for some sort of closed-loop control, but possibly for fault detection), then that delay can be a performance-killer. In addition, there's a feeling in the audio community that unwisely-designed constant-delay filters can cause aesthetic problems (Google on "pre-ring"). You solve these problems (and cause others) by using a minimum-phase filter. These are almost always done by using an IIR filter rather than an FIR.

$\endgroup$
  • $\begingroup$ But as I think about it now esp for img proc., if i set to 0 the frequency bin i want to reject in the magnitude of the fft, not touch phase part at all, then rebuild the image from ifft2(mag.*exp(i*phase)) it will just result in not having the contribution of the zeroed frequency. ... isnt it as simple as that? $\endgroup$ – Machupicchu Dec 23 '20 at 1:45
  • $\begingroup$ That's a different question. Yes an no -- it is as simple as that, but it's probably not what you want, particularly for image processing. Sharp cutoffs, such as you get by just zeroing out bins, causes lots of ringing at edges. Taking out just one bin will cause ringing across the entire image. This is really a separate question -- ask it, and someone will answer. $\endgroup$ – TimWescott Dec 23 '20 at 4:29
  • $\begingroup$ Well yes i think I know: Gibbs ringing due to sinc in image domain wrt Convolution theorem as the cutoff of 1 freq is like a tiny rectangle with a sinc as its FT and in img domain we have Convolution with this sinc. Sorry i didn't make the link immediately... i had the elements to answer my own question but I guess I wanted to see other's answers $\endgroup$ – Machupicchu Dec 23 '20 at 11:28
0
$\begingroup$

You should use the ‘symmetric’ flag in the ifft call. This will give you a real result. Or, you can do it yourself by imposing conjugate symmetry before taking the ifft (you’ll need to add another stopband centered at FS- f2). However, it’s still a bad way to implement a filter, as mentioned in the comments.

$\endgroup$
  • $\begingroup$ I understand, sorry, i forgot about the negative frequencies. Please see my new piece of code. It's more about getting an intuitive understanding than building a "good" filter. What I am really asking is which phases should I put in the filter and why. $\endgroup$ – Machupicchu Jul 31 '19 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.