Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

can someone help me understand this matlab code?

Status
Not open for further replies.

jake1

Newbie level 3
Joined
Oct 28, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,316
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
 

moonnightingale

Full Member level 6
Joined
Sep 17, 2009
Messages
362
Helped
5
Reputation
10
Reaction score
5
Trophy points
1,298
Activity points
3,832
Kindly goto Matlab and write
help freqz
u will come to know all abt this command
 
  • Like
Reactions: jake1

    jake1

    Points: 2
    Helpful Answer Positive Rating

jake1

Newbie level 3
Joined
Oct 28, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,316
Hi thanks for the answer

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?
 

jake1

Newbie level 3
Joined
Oct 28, 2011
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,316
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
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top