1

I have a matched filter, which I want to plot its frequency response in Matlab.

The filter response is: H(f) = Frequency response of wiener filter

I tried to to plot it with:

%Freqency_Response_of_wiener_filter
f = linspace(-1e3,1e3,1e5);
H = ((2*pi*f)^2+10^6)/(11*(2*pi*f)^2+10^6+10^4);
plot(f,H);
xlabel('f')
ylabel('H(f)')

which not working, giving me error of 'Matrix dimensions must agree' kind. I then read about 'element-wise power', which seems to fit exactly to what I need, and changed H to:

H = ((2*pi*f).^2+10^6)/(11*(2*pi*f).^2+10^6+10^4);

This indeed plot something, just not what I want :) I tried also

H = ((2*pi)^2*f.^2+10^6)/(11*(2*pi)^2*f.^2+10^6+10^4);

with no luck. The only way I got it working is:

%Freqency_Response_of_wiener_filter
f = linspace(-1e3,1e3,1e5);
for i=1:length(f)
  H(i) = ((2*pi*f(i))^2+10^6)/(11*(2*pi*f(i))^2+10^6+10^4);
end
plot(f,H);

Why is 'element-wise power' not working for me?

More than that - what exactly the differenece between regular operation to 'element-wise operation'? Because, for example, over here: An Introduction to Matlab, there's this plot:

a = 0:.01:5;
b = cos(2*pi*a);
plot(a,b)

and then this one:

x = 2:.1:4;
y = 1./x;
plot(x,y)
xlabel('x');
ylabel('y');

and I can't tell any difference between them. Why on the first one there was no need of 'element-wise operation', while in the second one there was?

Thanks.

Bak Itzik
  • 466
  • 1
  • 5
  • 17
  • 2
    Simple rule of thumb: when you want to do element wise operations: chuck in a dot before every `/`, `*` and `^`. It's a bit overkill, i.e. `2.*pi` is not doing anything else than `2*pi` since both are already scalars, but at least it'd save you from debugging these kind of hard-to-find errors. – Adriaan May 25 '16 at 06:23
  • 1
    I second Adriaan's comment. You don't need it to multiply a scalar by a matrix, but if you have two matrices (`x,y`) with a scalar (`k`) in between you might get into debugging problems: `x*k` and `k*y` both work, but `x*k*y` will cause an error. Instead of figuring out where to put the dot/dots, simply put it both places: `x.*k.*y`. – Stewie Griffin May 25 '16 at 06:43

1 Answers1

3

The reason why H = ((2*pi)^2*f.^2+10^6)/(11*(2*pi)^2*f.^2+10^6+10^4); did not work is because you need ./:

H = ((2*pi)^2*f.^2+10^6)./(11*(2*pi)^2*f.^2+10^6+10^4);

In the first case:

a = 0:.01:5;
b = cos(2*pi*a);
plot(a,b)

you do not need an element-wise-operation because there is only one way of doing the cosine of a vector or a matrix.

On the other hand, in another case:

x = 2:.1:4;
y = x.^2;
plot(x,y)
xlabel('x');
ylabel('y');

You need to specify that you want to make the element-wise operation rather than multiplying the matrix by itself (that works only for square matrices).

In the second case you post:

x = 2:.1:4;
y = 1./x;
plot(x,y)
xlabel('x');
ylabel('y');

you need the . so that Matlab understand that 1 has to be a vector of ones with length numel(x).

Stewie Griffin
  • 14,889
  • 11
  • 39
  • 70
shamalaia
  • 2,282
  • 3
  • 23
  • 35