Categories
- Basic Matlab Operations (19)
Pages
Followers
About Me
- santrox
- Kolkata, West Bengal, India
- well hey this is me.i am here to kill time.thought of starting with sharing my matlab programmings-i am not an expert in this field...am learning in on the way but with your support i can reach o a greater height.feel free to share your views and queries :)
Powered by Blogger.
Search This Blog
12:24 PM
function ndft()
%plots the frequency and phase spectrum of N samples of the signals
M=20; %no. of samples taken to plot each sample of the fequency and phase spectrum
N=40;
n=1:M;
x(n)=(0.5).^(n-1);
x(M+1:N)=0;
y(1:N)=0;
for k=1:N
for n=1:M
y(k)=y(k)+x(n)*exp(-j*2*pi*(k-1)*(n-1)/N);
end
end
k=0:N-1;
subplot(3,1,1);
stem(k,x(k+1));
title('input');
subplot(3,1,2);
stem(k,abs(y(k+1)));
title('frequency spectrum');
subplot(3,1,3);
stem(k,angle(y(k+1)));
title('phase spectrum');
CHANGE THE VALUE OF N AND SEE THE DIFFERENCE
PLEASE LEAVE A COMMENT :)
%plots the frequency and phase spectrum of N samples of the signals
M=20; %no. of samples taken to plot each sample of the fequency and phase spectrum
N=40;
n=1:M;
x(n)=(0.5).^(n-1);
x(M+1:N)=0;
y(1:N)=0;
for k=1:N
for n=1:M
y(k)=y(k)+x(n)*exp(-j*2*pi*(k-1)*(n-1)/N);
end
end
k=0:N-1;
subplot(3,1,1);
stem(k,x(k+1));
title('input');
subplot(3,1,2);
stem(k,abs(y(k+1)));
title('frequency spectrum');
subplot(3,1,3);
stem(k,angle(y(k+1)));
title('phase spectrum');
CHANGE THE VALUE OF N AND SEE THE DIFFERENCE
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
12:01 PM
function ndft()
%plots the frequency and phase spectrum of N samples of the signals
M=10; %no. of samples taken to plot each sample of the fequency and phase spectrum
A=input('Enter the magnitude ');
N=100;
n=1:M;
x(n)=A;
x(M+1:N)=0;
y(1:N)=0;
for k=1:N
for n=1:M
y(k)=y(k)+x(n)*exp(-j*2*pi*(k-1)*(n-1)/N);
end
end
k=0:N-1;
subplot(3,1,1);
stem(k,x(k+1));
title('input');
subplot(3,1,2);
stem(k,abs(y(k+1)));
title('frequency spectrum');
subplot(3,1,3);
stem(k,angle(y(k+1)));
title('phase spectrum');
PLEASE LEAVE A COMMENT :)
%plots the frequency and phase spectrum of N samples of the signals
M=10; %no. of samples taken to plot each sample of the fequency and phase spectrum
A=input('Enter the magnitude ');
N=100;
n=1:M;
x(n)=A;
x(M+1:N)=0;
y(1:N)=0;
for k=1:N
for n=1:M
y(k)=y(k)+x(n)*exp(-j*2*pi*(k-1)*(n-1)/N);
end
end
k=0:N-1;
subplot(3,1,1);
stem(k,x(k+1));
title('input');
subplot(3,1,2);
stem(k,abs(y(k+1)));
title('frequency spectrum');
subplot(3,1,3);
stem(k,angle(y(k+1)));
title('phase spectrum');
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
8:10 PM
I am bit under pressure cause our exams are banging at the door...lolz...However I thought of showing how to plot graphs.
1.ezplot('f(x)',[min max])
plots the given function within the range [min,max]
2.plot(x,f(x)) plots the given function y=f(x)
However in this case you need to use discrete values to define the range of x.
eg.,
n=0:0.1:2*pi;
plot(n,sin(n))
you can also write plot(f(x)) and produce the same result.In the above example matlab plots a sinusoidal graph by evaluating sin n for each value of n.n here is a range of discrete values with difference of 0.1.Hence in order to get a more continuous graph decrease the difference of n.Try It!
Now if you want to plot more than one graphs on one figure then use hold function.And if you want to get a better view of the graph use grid function.
Labels:
When you want to label the axes and the graph then use xlabel('name1'),ylabel('name2'),title('Title') functions.
Here's an example:
n=0:0.01:1;
grid on;
plot(5*n);
hold on;
plot(exp(n),'red')
hold off;
grid off;
xlabel('x->');ylabel('Amplitude');title('Overlapping graphs');
3.stem(n,y(n))
Sometimes we want to plot discrete values.This function plots the discrete values n points and their corresponding values.
PLEASE LEAVE A COMMENT :)
1.ezplot('f(x)',[min max])
plots the given function within the range [min,max]
2.plot(x,f(x)) plots the given function y=f(x)
However in this case you need to use discrete values to define the range of x.
eg.,
n=0:0.1:2*pi;
plot(n,sin(n))
you can also write plot(f(x)) and produce the same result.In the above example matlab plots a sinusoidal graph by evaluating sin n for each value of n.n here is a range of discrete values with difference of 0.1.Hence in order to get a more continuous graph decrease the difference of n.Try It!
Now if you want to plot more than one graphs on one figure then use hold function.And if you want to get a better view of the graph use grid function.
Labels:
When you want to label the axes and the graph then use xlabel('name1'),ylabel('name2'),title('Title') functions.
Here's an example:
n=0:0.01:1;
grid on;
plot(5*n);
hold on;
plot(exp(n),'red')
hold off;
grid off;
xlabel('x->');ylabel('Amplitude');title('Overlapping graphs');
3.stem(n,y(n))
Sometimes we want to plot discrete values.This function plots the discrete values n points and their corresponding values.
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
11:33 AM
1.Suppose we want the user to give an input.This we do using the following format:
x=input('statement' );
where x is assigned the input value.Here x is a real number.
Sometimes we want the user to give an input that may be some character or any string.In that case we use the following format:
x=input('statement','s');
Here x is assigned the character or string value.And this is indicated by 's' which stands for string type.
2.We have already seen a few examples where we want to produce some numerical output.However in case of some string we use the sprintf() or disp() function.
eg.,
sprintf('I know matlab')
or disp('I know matlab')
or frpintf('I know matlab')
gives the output:
ans=
I know matlab
3.Now suppose you want to display a value of any variable within a statement you can this as follows:
eg.,
i=5;
fprintf('the value for i is %d',i);
Here the output goes like this:
the value of i is 5
thus the %d is replaced by the value of i.Now if there are more than one variables then it's nothing different from the previous example:
eg.,
i=5;
j=8;
fprintf('%d is greater than %d',j,i);
Here the first %d is replaced by j and the second %d is replaced by i.Hence the output is:
8 is greater than 5
4.If you want to add a new line or a tab or a carriage return use the following symbols inside the fprintf function:
\n for new line,i.e.,goes to the next line and places the cursor at the beginning.
\t for a tab
\r for a carriage return,i.e., returning the cursor to the intial position of the row.
eg.,
if you want to display:
what is this?
i don't know
do it like this:
fprintf('what is this?\ni don't know\n');
The \n moves the cursor to the new line and prints the next words from the fresh start.
PLEASE LEAVE A COMMENT :)
x=input('statement' );
where x is assigned the input value.Here x is a real number.
Sometimes we want the user to give an input that may be some character or any string.In that case we use the following format:
x=input('statement','s');
Here x is assigned the character or string value.And this is indicated by 's' which stands for string type.
2.We have already seen a few examples where we want to produce some numerical output.However in case of some string we use the sprintf() or disp() function.
eg.,
sprintf('I know matlab')
or disp('I know matlab')
or frpintf('I know matlab')
gives the output:
ans=
I know matlab
3.Now suppose you want to display a value of any variable within a statement you can this as follows:
eg.,
i=5;
fprintf('the value for i is %d',i);
Here the output goes like this:
the value of i is 5
thus the %d is replaced by the value of i.Now if there are more than one variables then it's nothing different from the previous example:
eg.,
i=5;
j=8;
fprintf('%d is greater than %d',j,i);
Here the first %d is replaced by j and the second %d is replaced by i.Hence the output is:
8 is greater than 5
4.If you want to add a new line or a tab or a carriage return use the following symbols inside the fprintf function:
\n for new line,i.e.,goes to the next line and places the cursor at the beginning.
\t for a tab
\r for a carriage return,i.e., returning the cursor to the intial position of the row.
eg.,
if you want to display:
what is this?
i don't know
do it like this:
fprintf('what is this?\ni don't know\n');
The \n moves the cursor to the new line and prints the next words from the fresh start.
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
6:26 AM
1.If there are algebraic functions which are needed to be multiplied,then simply writing the expressions and pressing enter won't give you the answer.Rather it will show the same as the input.
Solution:-
Firstly you have to define the variables and for finding out the product of two expressions we will use the function expand().
eg.,
>> syms x y
>> expand((x+y)*(x-y))
gives you
ans =
x^2-y^2
2.Now in order to factorize an algebraic expression we use the function factor()
eg.,
>> syms x y
>> factor(x^2-y^4)
gives you
ans =
(x-y^2)*(x+y^2)
3.Lastly in order to find out the quotient when an algebraic expression is divided by another, we use the simplify() function.
eg.,>> simplify((x^3-y^3)/(x-y))
The solution is :
ans =
x^2+x*y+y^2
But in order to get any expression in its simplest form we use simple() function sometimes.
PLEASE LEAVE A COMMENT :)
Solution:-
Firstly you have to define the variables and for finding out the product of two expressions we will use the function expand().
eg.,
>> syms x y
>> expand((x+y)*(x-y))
gives you
ans =
x^2-y^2
2.Now in order to factorize an algebraic expression we use the function factor()
eg.,
>> syms x y
>> factor(x^2-y^4)
gives you
ans =
(x-y^2)*(x+y^2)
3.Lastly in order to find out the quotient when an algebraic expression is divided by another, we use the simplify() function.
eg.,>> simplify((x^3-y^3)/(x-y))
The solution is :
ans =
x^2+x*y+y^2
But in order to get any expression in its simplest form we use simple() function sometimes.
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
10:34 AM
Here we are now going to solve equations.
1.Solving an equation with one variable:
solve('equation') will solve the equation(linear or non-linear) for the given variable.
eg., solve('5*x+x^2=9') gives the answers
-5/2+1/2*61^(1/2)
-5/2-1/2*61^(1/2)
2.Solving equations with more than one variables
eg.,simultaneous equations with two variables x,y
[x,y]=solve('equation_1','equation_2')
This solves the two equations(linear or non-linear). two the results of the two variables.If there are more than one solutions for each variable,eg.
x=
5
6
y=
7
8
then (5,7) and (6,8) are the two solutions of the equations.
3.Expressing one variable in terms of the other:
solve('equation','subjective variable')
eg. solve('5*x-1=y','x') expresses x in terms of y
therefore the solution is:
ans =
1/5+1/5*y
1.Solving an equation with one variable:
solve('equation') will solve the equation(linear or non-linear) for the given variable.
eg., solve('5*x+x^2=9') gives the answers
-5/2+1/2*61^(1/2)
-5/2-1/2*61^(1/2)
2.Solving equations with more than one variables
eg.,simultaneous equations with two variables x,y
[x,y]=solve('equation_1','equation_2')
This solves the two equations(linear or non-linear). two the results of the two variables.If there are more than one solutions for each variable,eg.
x=
5
6
y=
7
8
then (5,7) and (6,8) are the two solutions of the equations.
3.Expressing one variable in terms of the other:
solve('equation','subjective variable')
eg. solve('5*x-1=y','x') expresses x in terms of y
therefore the solution is:
ans =
1/5+1/5*y
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
9:57 AM
If we execute sin (pi) we should get zero as the answer,however the value of sin(pi) when executed in matlab shows a value of 1.2246e-016.The reason is that pi here is not the irrational number rather an approximate value of 3.141592653589793.
Hence in order to use the irrational value of pi we have to execute it as a symbol.
sin(sym('pi'))
This gives a value of zero.This is so because pi here is treated as a symbol which has the irrational value.It should be written as sym('pi').
PLEASE LEAVE A COMMENT :)
Hence in order to use the irrational value of pi we have to execute it as a symbol.
sin(sym('pi'))
This gives a value of zero.This is so because pi here is treated as a symbol which has the irrational value.It should be written as sym('pi').
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
1:55 PM
here we shall start learning matlab from the basic stuffs.
1. n=0:10
This means that n is a one-dimensional array or a row matrix with values taking from 0 to 10 with an interval of 1,i.e.,[0 1 2 3 ... 9 10]
2.n=0:0.5:10
This means that n is a one-dimensional array or a row matrix with values from 0 to 10 with an interval of 0.5,i.e.,[0 0.5 1 1.5 ... 9.5 10]
3.use of a ;
If we put a semicolon at the end of an equation or an expression matlab does not display the resulting values.And if we want to see the values we DO NOT put a semicolon.
4.x(n)
x(n) is an array of n elements under the family named x.each number in the array is allocated an address but the address starts from 1 and ends at n.So lets do a program that allocates values to the array x(n) to each address.
n=1:10;
x(n)=n;
This program stores the value n at each of the nth address of the array.
Therefore x=[1 2 3 4 5 6 7 8 9 10]
So if you wanna see the values allocated to the array do either of the following:
1.Remove the ; from the second line of the above programming.
2.Type x and press enter in the command window.
Now if you wanna see the value stored at the address location 5 simply type x(5) and press enter.See the result.
5.syms
This is used to initialize a variable or variables as a symbol or symbols.
syms x y indicates x and y are initialized as symbols or variables.
6.whos
Executing this shows all the variables used,the space allocated to them,the class.If you wanna clear a variable name so type clear x if you wanna clear x.if u wanna clear two variables x and y execute clear x y.
If you wanna clear all type clear all
PLEASE LEAVE A COMMENT :)
5.syms
This is used to initialize a variable or variables as a symbol or symbols.
syms x y indicates x and y are initialized as symbols or variables.
6.whos
Executing this shows all the variables used,the space allocated to them,the class.If you wanna clear a variable name so type clear x if you wanna clear x.if u wanna clear two variables x and y execute clear x y.
If you wanna clear all type clear all
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
8:26 AM
we all know cos x is periodic with a period of 2*n*pi.so if two signals are sampled at a rate less than the Nyquist rate then we can have same samples from two different signals.The reason is as follows:
lets say sampling frequency is Fs.Hence Ts=1/Fs.
if a signal say cos(2*pi*f*t) is sampled then we get the sampled version as cos(2*pi*F*n*Ts)
now lets take another signal cos(2*pi*(f+Fs)*t).If this is sampled at the same rate then we get:
cos(2*pi*(f+Fs)*n*Ts)=cos(2*pi*f*n*Ts+2*pi*Fs*n*Ts) now the term 2*pi*n*Fs*Ts yields nothing but 2*pi*n.Since cos x is periodic with period 2*pi*n hence the second sampled signal reduces to cos(2*pi*f*n*Ts) which is the same sampled signal as the previous one.
PROBLEM :
Four sinusoidal signals are sampled and they are found to have same sampled version in pairs.Now you know why.
function sinseq1()
A=2;
phi=0;
n=0:10;
%case 1
x=A*cos(2*pi*1.1*n+phi);
subplot(2,2,1);
stem(n,x);
%case 2
x=A*cos(2*pi*0.9*n+phi);
subplot(2,2,2);
stem(n,x);
%case 3
x=A*cos(2*pi*0.8*n+phi);
subplot(2,2,3);
stem(n,x);
%case 4
x=A*cos(2*pi*1.2*n+phi);
subplot(2,2,4);
stem(n,x);
PLEASE LEAVE A COMMENT :)
lets say sampling frequency is Fs.Hence Ts=1/Fs.
if a signal say cos(2*pi*f*t) is sampled then we get the sampled version as cos(2*pi*F*n*Ts)
now lets take another signal cos(2*pi*(f+Fs)*t).If this is sampled at the same rate then we get:
cos(2*pi*(f+Fs)*n*Ts)=cos(2*pi*f*n*Ts+2*pi*Fs*n*Ts) now the term 2*pi*n*Fs*Ts yields nothing but 2*pi*n.Since cos x is periodic with period 2*pi*n hence the second sampled signal reduces to cos(2*pi*f*n*Ts) which is the same sampled signal as the previous one.
PROBLEM :
Four sinusoidal signals are sampled and they are found to have same sampled version in pairs.Now you know why.
function sinseq1()
A=2;
phi=0;
n=0:10;
%case 1
x=A*cos(2*pi*1.1*n+phi);
subplot(2,2,1);
stem(n,x);
%case 2
x=A*cos(2*pi*0.9*n+phi);
subplot(2,2,2);
stem(n,x);
%case 3
x=A*cos(2*pi*0.8*n+phi);
subplot(2,2,3);
stem(n,x);
%case 4
x=A*cos(2*pi*1.2*n+phi);
subplot(2,2,4);
stem(n,x);
PLEASE LEAVE A COMMENT :)
Labels: Basic Matlab Operations
Subscribe to:
Posts (Atom)