# Manually creating a lowpass filter from the frequency domain: what phase?

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:

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 )

• That's a bad idea and won't work. See dsp.stackexchange.com/questions/6220/… – Hilmar Jul 30 '19 at 18:16
• Possible duplicate of Why is it a bad idea to filter by zeroing out FFT bins? – Marcus Müller Jul 30 '19 at 21:40
• @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" – Machupicchu Jul 31 '19 at 7:59
• @MarcusMüller I don't think so, since I am mostly interested about the phase, as I have said. – Machupicchu Jul 31 '19 at 8:05
• 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 – Hilmar Jul 31 '19 at 9:33