# can someone help me understand this matlab code?

Status
Not open for further replies.

#### 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:

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?

#### moonnightingale

##### Full Member level 6
Kindly goto Matlab and write
help freqz
u will come to know all abt this command

jake1

### jake1

Points: 2

#### jake1

##### Newbie level 3

I find it difficult to understand why the graph changes for different values of M, for example for big M the graph becomes a line for small M it's close to the initial graph, can you explain me why this is happening? does this have anything to do with aliasing?

#### lebesgue

##### Member level 3
You know what a downsampler is?

#### jake1

##### Newbie level 3
Yes I think so,

for example if we have a signal in the time domain with a maximum frequency of 2 Hz

and we sample it with a frequency of 1 Hz, we down sample the signal because according to Nyquist we need to sample with a frequency of at least 2*2 = 4 Hz in order to be able to obtain the whole initial analog signal after converting it into digital first

#### lebesgue

##### Member level 3
Thats the exact of using an upsampler.

Status
Not open for further replies.

Replies
4
Views
867
Replies
1
Views
2K
Replies
5
Views
1K
Replies
3
Views
1K
Replies
5
Views
12K