# How to use the iircomb filter in matlab

I have a sound file that needs filtering which looks like this: Hence I think that I have noise spaced equally at 832Hz. i tried to use the iircomb filter taken from mathworks website and i managed to create this:

clear
fs = 44100;
fo = fs/53;
q = 35;
bw = (fo/(fs/2))/q;
[b,a] = iircomb(fs/fo,bw,'notch'); % Note type flag 'notch'
fvtool(b,a);
fnew=filter(b,a,f)

N = size(f,1);
df = fs / N;
w = (-(N/2):(N/2)-1)*df;
y = fft(fnew(:,1), N) / N; % For normalizing, but not needed for our analysis
y2 = fftshift(y);
figure(2);
plot(w,abs(y2));
p = audioplayer(fnew,fs);
p.play;


Now, the filter works pretty good but there is still a little bit of noise after filtering which can be heard. Should I just live with it, or perhaps my filter could be improved? Could someone with more experience work on it for a minute or two? The sound file is only 3 seconds long.

Here is the audio file if anyone needs it: SOUND FILE

Thank you for help.

• you're trying to take out that obnoxious tone and leave the acoustic guitar, right? one problem is that you may need to tune your comb filter better. you may need for a delay element, one having fractional sample precision. the period of that obnoxious tone might not be exactly an integer number of samples. BTW, i wouldn't bother with iircomb() as a function. just design and implement the comb filter yourself with a precision delay element. – robert bristow-johnson Jan 3 '19 at 0:34
• yes that's precisely what I am trying to do. Would you be able to explain to me how to add this delay element? uk.mathworks.com/help/dsp/ref/iircomb.html says nothing about it, and I am really really new to this kind of stuff. Thank you either way. And as soon as f0 stops being an integer, matlab throws an error at me. Perhaps there's a way around it? – Scavenger23 Jan 3 '19 at 0:48

Precision delay is, in my opinion, best understood in terms of the Nyquist-Shannon sampling and reconstruction theorem.

If a continuous-time (a.k.a. "analog", but that is somewhat imprecise) signal $$x(t)$$ is bandlimited to below the Nyquist frequency $$\frac{1}{2T}$$ (where $$T$$ is the sampling period and $$f_\mathrm{s} \triangleq \frac1T$$ is the sample rate), then the original continuous-time signal $$x(t)$$ can be reconstructed from the discrete samples $$x[n]$$ as:

\begin{align} x(t) &= \sum_{n=-\infty}^{\infty} x(nT) \, \operatorname{sinc}\left(\tfrac{t - nT}{T}\right) \\ &= \sum_{n=-\infty}^{\infty} x[n] \, \operatorname{sinc}\left(\tfrac{t - nT}{T}\right) \\ \end{align}

where

$$\operatorname{sinc}(u) \triangleq \begin{cases} \frac{\sin(\pi u)}{\pi u}, & \text{if } u \ne 0 \\ 1, & \text{if } u = 0 \\ \end{cases}$$

All terms of the summation are bandlimited to a maximum frequency of $$\frac{1}{2T}$$, so the summation is bandlimited to the same bandlimit as the original $$x(t)$$. The samples are

$$x[n] \triangleq x(t) \Bigg|_{t = nT} \triangleq x(nT)$$

Because $$x(t)$$ can be evaluated at any $$t$$, not only multiples of $$T$$, this is the basis in which we interpolate to get a precision delay.

Suppose we wanna delay by an amount of time $$\tau$$, then the output of the delay is

\begin{align} y(t) &= x(t-\tau) \\ &= \sum_{n=-\infty}^{\infty} x[n] \, \operatorname{sinc}\left(\tfrac{t-\tau - nT}{T}\right) \\ \end{align}

So that is explicitly where you get the delayed signal of $$y(t) = x(t-\tau)$$ from the samples $$x[n]$$. The first problem we have is that we cannot add up an infinite number of terms. So our first practical approximation is truncating terms. But that is the same as applying a rectangular window (usually considered to be the worst one), so instead, we'll apply a better window.