# FIR filter design: evaluating the error function

Question 1

$$H(e^{j\omega})=\sum_{n=0}^{N-1}h[n]e^{-jn\omega} =\mathbf{c}^H(\omega)\cdot \mathbf{h} \tag{1}$$ $$=\mathbf{h}^H\cdot\mathbf{c}(\omega) \tag{2}$$

$$H(\mathbf{h})=\sum_{k=1}^Kh[k]e^{j\omega_k}\tag{3}$$

The design of filter in matlab ,if using:

$-$$(1) and (3) the length of filter is K. -$$(2)$ the length of filter is $N$.

• What is the difference between these two design?
• Can i use the operation 'transpose' in $D(w)$ and weighting function,length of filter?

Question 2

application in matlab

I used the same functions in 'cfirls' like desired and actual function.

   %% Desired coefficient

N = 61;              %desired filter length
tau = 26;            % desired passband group delay
% frequency grid
f = [linspace(-1,-.18,164),linspace(-.1,.3,80),linspace(.38,1,124)];
% desired frequency response
d = [zeros(1,164),ones(1,80),zeros(1,124)].*exp(-1i*pi*f*tau);
w = [10*ones(1,164),ones(1,80),10*ones(1,124)];
A = w(:,ones(1,N)) .* exp(-1i*pi*f*(0:N-1));
h = A \ (w.*d);
%%actual function
c=[zeros(1,164),ones(1,80),zeros(1,124)]*exp(-1i*pi*f*(0:N-1));
H=h'.*c;

%% error function
E=abs(H-d)^2;
S=sum(E');

plot(f,S)


I need in my work to reduce the error as little as possible.How can i changing in this code?

• Does your code actually run or does it produce an error? Aug 16, 2018 at 17:02
• E=abs(H-D)^2; ... where this variable D comes from? Aug 16, 2018 at 17:22
• @ Matt L , @Juha P : I edited the question, the code actually run . Aug 16, 2018 at 17:36
• When I try it the code doesn't run, because the matrix dimensions are wrong. Apart from that, what is the problem? How do you know it's not the "best design"? Why is D different from d? You use d to design the filter and then you use D to compute the error, this doesn't make much sense. Aug 16, 2018 at 17:49

There are many problems in your question and in your code. First, Eqs $(2)$ and $(3)$ are wrong. Eq. $(2)$ cannot equal Eq. $(1)$ because it is its complex conjugate (and $\mathbf{c}$ is a complex vector). Eq. $(3)$ is meaningless.

I will not rewrite your Matlab code, but you should have added that it doesn't run. There are many errors, among which several incompatible matrix dimensions. You should use the function cfirls instead of just copying some of its code. Then vector dimensions would be checked and care would be taken that all vectors have the same format (I chose columns).

Assuming that all these problems are resolved (simply by calling cfirls with the vectors f, d, and w), you can compute the error, but this is where the next problem occurs. Why don't you just use Eq. $(1)$ in your question to compute the frequency response? (You might as well use the Matlab command freqz). The way you define the matrix c in order to compute the frequency response H doesn't make sense.

Assuming that you manage to compute the designed frequency response on a grid, you could choose to compute the unweighted error, as suggested by your code (even though you minimized a weighted error). However, your variable S will be a scalar (as soon as the code to compute it is fixed). So how do you want to plot it as a function of frequency (as suggested by the last line of your code)?

In sum, apart from using cfirls, which will give you the correct filter coefficients, think about how you should compute the frequency response, and why you defined the matrix c the way you did. Then think about which quantity you would like to plot. As mentioned before, the squared error is just a number, not a function of frequency. It could be useful to plot the magnitude of the difference between desired and designed frequency response:

$$\big|E(\omega)\big|=\big|H(\omega)-D(\omega)\big|\tag{1}$$

With N, f, d, and w defined as in your code, the most straightforward way to do what you're trying to do would be:

h = cfirls(N,f,d,w);
H = freqz(h,1,pi*f);
E = H - d;
plot(f,20*log10(abs(E)))

• L.thank you for your clear answer,but about the equation (1) i found in certain article is the same (2) ,but with using the transpose operation,is the 'transpose' and 'Hermitien' are similar? Aug 19, 2018 at 8:38
• The symbol $^H$ means that you transpose and conjugate the vector. This is why Eqs (1) and (2) are not the same. Of course, for any column vectors $a$ and $b$ you have $a^Tb=b^Ta$, but $a^Hb=(b^Ha)^*$, where $*$ means conjugation. Aug 19, 2018 at 9:38
• L:in my case can i use just the transpose operation ? Aug 19, 2018 at 10:24
• @K.n90: Yes, if you transpose you get $\mathbf{h}^T\mathbf{c}^*$. Aug 19, 2018 at 10:29