#### jake1

##### Newbie level 3

Code:

```
freq = [0 0.42 0.48 1];
mag = [0 1 0 0];
x = fir2(101, freq, mag);
% Evaluate and plot the input spectrum
[Xz, w] = freqz(x, 1, 512);
plot(w/pi, abs(Xz)); grid
xlabel('\omega/\pi'); ylabel('Magnitude');
title('Input spectrum');
pause
% Generate the down-sampled sequence
M = input('Type in the down-sampling factor = ');
y = x([1: M: length(x)]);
% Evaluate and plot the output spectrum
[Yz, w] = freqz(y, 1, 512);
plot(w/pi, abs(Yz)); grid
xlabel('\omega/\pi'); ylabel('Magnitude');
title('Output spectrum');
```

I got it from a book I'm currently studying and I can't understand some things about this code. For example what does the function fir2 and freqz do? And what kind of changes am I supposed to see after I give M as an input to the program?

I'm new to digital processing and I understand the various transformations that can be applied to signals it's just that it's quite difficult to interpret the transformations in Matlab and that's why I'm asking you guys here. I tried reading the documentation but I still can't seem to connect everything together.

My explanation so far:

basically we start with these two lines

Code:

```
freq = [0 0.42 0.48 1];
mag = [0 1 0 0];
```

I suppose that they are being used in the fir2 function. The first one creates a vector where we have values that correspond to normalized frequencies. The second vector tells us what the amplitude of the frequency respond would be at the given frequency. For example at 0.42 it will be 1 at the other frequencies it will go down to 0.

Code:

`x = fir2(101, freq, mag);`

here we create a 100-order FIR system, and x is the impulse response of the system, correct?

from here I can't understand what we're doing

Code:

```
[Xz, w] = freqz(x, 1, 512);
plot(w/pi, abs(Xz)); grid
```

what are xz? w? and what does freqz do? how about the values 512????

later we use M and after that we do the same thing and plot a new graph of the frequency response, but why am I getting different results for different M and how can I somehow predict the next result by knowing the result at some value of M?

thanks in advance