Sns Lab Manual
Sns Lab Manual
Scope:
Requirement:
Theory:
GNU Octave is a high-level language primarily intended for numerical computations. It is typically used
for such problems as solving linear and nonlinear equations, numerical linear algebra, statistical analysis,
and for performing other numerical experiments. It may also be used as a batch-oriented language for
automated data processing.
The current version of Octave executes in a graphical user interface (GUI). The GUI hosts an Integrated
Development Environment (IDE) which includes a code editor with syntax highlighting, built-in debugger,
documentation browser, as well as the interpreter for the language itself. A command-line interface for
Octave is also available.
Sample Codes
1. Elementary Calculations
Octave can easily be used for basic numerical calculations. Octave knows about arithmetic
operations (+, −,∗,/), exponentiation (^), natural logarithms/exponents (𝑙𝑜𝑔, 𝑒𝑥𝑝), and the
trigonometric functions (𝑠𝑖𝑛, 𝑐𝑜𝑠, … ). Moreover, Octave calculations work on real or imaginary
numbers (𝑖, 𝑗). In addition, some mathematical constants such as the base of the natural logarithm
(𝑒) and the ratio of a circle’s circumference to its diameter (𝑝𝑖) are pre-defined.
For example, to verify Euler’s Identity,
To display the value of a variable, simply type the name of the variable at the prompt. For example,
to display the value stored in the matrix B, type the command
B
3. Matrix Arithmetic
Octave uses standard mathematical notation with the advantage over low-level languages that
operators may act on scalars, vector, matrices, or N-dimensional arrays. For example, to multiply
the matrix A by a scalar value, type the command
octave:4> 2 * A
and to form the matrix product transpose (A) * A, type the command
octave:6> A' * A
will create a file called foo.pdf that contains a rendering of the current plot in Portable Document
Format. The command
help print
explains more options for the print command and provides a list of additional output file formats
5. Indexing Expressions
An index expression allows you to reference or extract selected elements of a vector, a matrix (2-
D), or a higher-dimensional array.
Indices may be scalars, vectors, ranges, or the special operator ‘:’, which selects entire rows,
columns, or higher-dimensional slices.
An index expression consists of a set of parentheses enclosing M expressions separated by
commas. Each individual index value, or component, is used for the respective dimension of the
object that it is applied to. In other words, the first index component is used for the first
dimension (rows) of the object, the second index component is used for the second dimension
(columns) of the object, and so on. The number of index components M defines the
dimensionality of the index expression. An index with two components would be referred to as
a 2-D index because it has two dimensions.
In the simplest case, 1) all components are scalars, and 2) the dimensionality of the index
expression M is equal to the dimensionality of the object it is applied to. For example:
A = reshape (1:8, 2, 2, 2) # Create 3-D array
A =
ans(:,:,1) =
1 3
2 4
ans(:,:,2) =
5 7
6 8
The size of the returned object in a specific dimension is equal to the number of elements in the
corresponding component of the index expression. When all components are scalars, the result
is a single output value. However, if any component is a vector or range then the returned values
are the Cartesian product of the indices in the respective dimensions. For example:
A([1, 2], 1, 2) ≡ [A(1,1,2); A(2,1,2)]
⇒
ans =
5
6
The total number of returned values is the product of the number of elements returned for each
index component. In the example above, the total is 2*1*1 = 2 elements.
Notice that the size of the returned object in a given dimension is equal to the number of elements
in the index expression for that dimension. In the code above, the first index component ([1, 2])
was specified as a row vector, but its shape is unimportant. The important fact is that the
component specified two values, and hence the result must have a size of two in the first
dimension; and because the first dimension corresponds to rows, the overall result is a column
vector.
A(1, [2, 1, 1], 1) # result is a row vector: ans = [3, 1, 1]
A(ones (2, 2), 1, 1) # result is a column vector: ans = [1; 1; 1; 1]
The first line demonstrates again that the size of the output in a given dimension is equal to the
number of elements in the respective indexing component. In this case, the output has three
elements in the second dimension (which corresponds to columns), so the result is a row vector.
The example also shows how repeating entries in the index expression can be used to replicate
elements in the output. The last example further proves that the shape of the indexing component
is irrelevant, it is only the number of elements (2x2 = 4) which is important.
The above rules apply whenever the dimensionality of the index expression is greater than one
(M > 1). However, for one-dimensional index expressions special rules apply and the shape of
the output is determined by the shape of the indexing component. For example:
A([1, 2]) # result is a row vector: ans = [1, 2]
A([1; 2]) # result is a column vector: ans = [1; 2]
Note that it is permissible to use a 1-D index with a multi-dimensional object (also called linear
indexing). In this case, the elements of the multi-dimensional array are taken in column-first
order like Fortran. That is, the columns of the array are imagined to be stacked on top of each
other to form a column vector and then the single linear index is applied to this vector.
A(5) # linear indexing into three-dimensional array: ans = 5
A(3:5) # result has shape of index component: ans = [3, 4, 5]
A colon (‘:’) may be used as an index component to select all of the elements in a specified
dimension. Given the matrix,
A = [1, 2; 3, 4]
all of the following expressions are equivalent and select the first row of the matrix.
A(1, [1, 2]) # row 1, columns 1 and 2
A(1, 1:2) # row 1, columns in range 1-2
A(1, :) # row 1, all columns
When a colon is used in the special case of 1-D indexing the result is always a column vector.
Creating column vectors with a colon index is a very frequently encountered code idiom and is
faster and generally clearer than calling reshape for this case.
A(:) # result is column vector: ans = [1; 2; 3; 4]
A(:)' # result is row vector: ans = [1, 2, 3, 4]
In index expressions the keyword end automatically refers to the last entry for a particular
dimension. This magic index can also be used in ranges and typically eliminates the needs to
call size or length to gather array bounds before indexing. For example:
A(1:end/2) # first half of A => [1, 2]
A(end + 1) = 5; # append element
A(end) = []; # delete element
A(1:2:end) # odd elements of A => [1, 3]
A(2:2:end) # even elements of A => [2, 4]
A(end:-1:1) # reversal of A => [4, 3, 2, 1]
User Interface of Octave
Installation Link
https://www.gnu.org/software/octave/download.html
Conclusion:
Scope: Learn Different commands on Matlab and Learn about how to write a script in Matlab.
Theory: Script is different than programming language. Script is similar to procedural programming
language, where we call the required function to perform certain task. Script helps us to execute the
functions created by us and run the code.
Subplot is command to divide plot window into number of sub parts. Because of subplot we can plot more
than one plot in same plot window.
Standard signals that need to be generated are given inside a library adsp_gui1 and adsp_mFiles1. To
execute any function given in the library we must have this library in Current Working Directory. If
execution is done from another location it will throw error such as function does not exist. In case of
MATLAB one need to add respective folder to the path to use the functions given in the respective folders.
1. Impulse Signal
1 𝑡=0
𝛿(𝑡) = {
0 𝑡≠0
3. Ramp Signal
𝑡 𝑡≥0
𝑟(𝑡) = {
0 𝑡<0
𝑥(𝑡) = eat
5. Rectangular Pulse
𝑥(𝑡) = 𝑢(𝑡) − 𝑢(𝑡 − 3)
6. Triangular Pulse
𝑥(𝑡) = 𝑡𝑟𝑖(𝑡)
7. Sinusoidal (sine and cosine)
𝑥(𝑡) = sin(𝑡)
𝑥(𝑡) = cos(𝑡)
Code:
clc;
clear all;
t = -10:0.01:10;
subplot(3,3,1)
axis([-10 10 -10 10]);
plot(t,ustep(t));
title('Step signal');
xlabel('t');
ylabel('f(t)');
subplot(3,3,2)
plot(t,udelta(t));
title('Impulse Signal')
xlabel('t');
ylabel('Sin(t)');
subplot(3,3,3)
plot(t,sin(t));
title('SINE Signal')
xlabel('t');
ylabel('r(t)');
subplot(3,3,4)
plot(t,uramp(t));
title('Ramp Signal')
xlabel('t');
ylabel('Cos(t)');
subplot(3,3,5)
plot(t,cos(t));
title('Cosine Signal')
xlabel('t');
ylabel('x(t)');
subplot(3,3,6)
a = 1;
y = exp((a*t))
plot(t,y);
title('Rising EXP Signal')
xlabel('t');
ylabel('x(t)');
subplot(3,3,7)
a = -1;
y = exp((a*t))
plot(t,y);
title('Decaying EXP Signal')
xlabel('t');
ylabel('x(t)');
subplot(3,3,8)
plot(t,(ustep(t-2) - ustep(t-8)) );
title('REC Window Signal')
xlabel('t');
ylabel('x(t)');
subplot(3,3,9)
p1 = 0.5*uramp(t).*(ustep(t)-ustep(t-2));
p2 = 0.25*uramp(-t+6).*(ustep(t-2)-ustep(t-6));
plot(t, p1+p2 );
title('TRI Window Signal')
Conclusion:
Scope: Learn Different commands on matlab and Learn about how to write a script in Matlab.
Theory: Script is different than programming language. Script is similar to procedural programming
language, where we call the required function to perform certain task. Script helps us to execute the
functions created by us and run the code.
Subplot is command to divide plot window into number of sub parts. Because of subplot we can plot more
than one plot in same plot window.
Syntax: subplot(row,col,position)
Standard signals that need to be generated are given inside a library adsp_gui1 and adsp_mFiles1. To
execute any function given in the library we must have this library in Current Working Directory. If
execution is done from another location it will throw error such as function does not exist. In case of
MATLAB one need to add respective folder to the path to use the functions given in the respective folders.
8. Impulse Signal
1 𝑛=0
𝛿[𝑛] = {
0 𝑛≠0
𝑥[𝑛] = 𝑎𝑛
clc;
clear all;
t = [-10:10];
subplot(3,3,1)
stem(t,ustep(t));
xlabel('t');
ylabel('x(t)');
title('step signal');
subplot(3,3,2)
stem(t,udelta(t));
xlabel('t');
ylabel('x(t)');
title('Impulse Signal');
subplot(3,3,3)
stem(t,sin( (t*pi)/6 ) );
xlabel('t');
ylabel('x(t)');
title('SINE Signal');
subplot(3,3,4)
stem(t,uramp(t));
xlabel('t');
ylabel('x(t)');
title('Ramp Signal');
subplot(3,3,5)
stem(t,cos(t*pi/6));
xlabel('t');
ylabel('x(t)');
title('Cosine Signal');
subplot(3,3,6)
a=1;
y = exp(a*t);
stem(t,y);
xlabel('t');
ylabel('x(t)');
title('Rising EXP Signal');
subplot(3,3,7)
a=-1;
y = exp(a*t);
stem(t,y);
xlabel('t');
ylabel('x(t)');
title('Decaying EXP Signal');
subplot(3,3,8)
stem(t,(ustep(t-2) - ustep(t-8)));
xlabel('t');
ylabel('x(t)');
title('REC window Signal');
subplot(3,3,9)
p1 = 0.5*uramp(t).*(ustep(t)-ustep(t-2));
p2 = 0.25*uramp(-t+6).*(ustep(t-2)-ustep(t-6));
stem(t, p1+p2 );
xlabel('t');
ylabel('x(t)');
title('TRI window Signal');
Conclusion:
DISCRETE TIME
t= -10:10;
subplot(2,2,1);
x1= sin(0.4*pi*t);
xlabel('Time');
ylabel('f(t)');
stem(t,x1);
grid on;
subplot(2,2,2);
x2= sin(0.6*pi*t);
xlabel('Time');
ylabel('f(t)');
stem(t,x2);
grid on;
subplot(2,2,3);
x3= x1+x2;
xlabel('Time');
ylabel('f(t)');
stem(t,x3);
grid on;
subplot(2,2,4);
x4= x1.*x2;
xlabel('Time');
ylabel('f(t)');
stem(t,x4);
grid on;
CONCLUSION:
RESULT:
CONTINUOUS TIME
DISCRETE TIME
PRACTICAL 5
AIM: To operate folding scaling and shifting on functions
CODE:
Function:
function y = fun4 (t);
x1=2*(ustep(t)-ustep(t-2));
x2=2*uramp(t-1).*(ustep(t-2)-ustep(t-3));
x3=2*uramp(5-t).*(ustep(t-3)-ustep(t-4));
x4=2*(ustep(t-4)-ustep(t-6));
y=x1+x2+x3+x4;
Code:
clc;
clear all;
t= -15:0.01:15;
a= fun4(t);
subplot(3,3,1);
xlabel('Time');
ylabel('f(t)');
plot(t,a);
grid on;
b=fun4(2*t);
c=fun4(0.5*t);
d = fun4(t-3);
e= fun4(t+3);
f= fun4(-t-3);
g = fun4(-t+3);
h = fun4((-1)*3*(t)+3);
i = fun4((0.33)*(t)-3);
subplot(3,3,2);
xlabel('Time');
ylabel('f(t)');
plot(t,b);
grid on;
subplot(3,3,3);
xlabel('Time');
ylabel('f(t)');
plot(t,c);
grid on;
subplot(3,3,4);
xlabel('Time');
ylabel('f(t)');
plot(t,d);
grid on;
subplot(3,3,5);
xlabel('Time');
ylabel('f(t)');
plot(t,e);
grid on;
subplot(3,3,6);
xlabel('Time');
ylabel('f(t)');
plot(t,f);
grid on;
subplot(3,3,7);
xlabel('Time');
ylabel('f(t)');
plot(t,g);
grid on;
subplot(3,3,8);
xlabel('Time');
ylabel('f(t)');
plot(t,h);
grid on;
subplot(3,3,9);
xlabel('Time');
ylabel('f(t)');
plot(t,i);
grid on;
CONCLUSION:
RESULT:
PRACTICAL 6
AIM: To find the odd and even part of a given signal and then
obtaining original signal using odd and even part.
CODE:
Function:
function a= fun3 (t);
a1=ustep(t+1)-ustep(t);
a2=uramp(t+1).*(ustep(t)-ustep(t-1));
a3=3*(ustep(t-1)-ustep(t-2));
a= a1+a2+a3;
endfunction
Code:
t=-10:0.01:10;
gxt=fun3(t);
fgxt=fun3(-t);
egxt=0.5*(gxt+fgxt);
ogxt=0.5*(gxt-fgxt);
orgxt=egxt+ogxt;
subplot(3,2,1);
plot(t,gxt);
grid on;
subplot(3,2,2);
plot(t,fgxt);
grid on;
subplot(3,2,3);
plot(t,egxt);
grid on;
subplot(3,2,4);
plot(t,ogxt);
grid on;
subplot(3,2,5);
plot(t,orgxt);
grid on
RESULT:
CONCLUSION:
PRACTICAL 7
AIM: To perform convolution in discrete time domain.
CODE:
WITHOUT ZERO PADDIND
clc;
clear all;
x=[1,2,3,4,0,0,0,0];
h=[5,6,7,8,0,0,0,0];
y=[0,0,0,0,0,0,0,0];
t=0:1:10;
m= xll+hll;
n=xul+hul;
for i= 1:n
for j= 1:n
if(i>=j)
y(i) = y(i) + x(j).*h(i-j+1) ;
end
j=j+1;
end
i=i+1;
end
stem(y,t);
lenx=length(x);
lenh=length(h);
leny=lenx+lenh-1;
y=zeros(1,leny);
xn=[x zeros(1,lenh-1)]
hn=[h zeros(1,lenx-1)]
for n=1:leny
for k=1:leny
if(n-k>=0)
y(n)=y(n)+xn(k)*hn(n-k+1);
end
end
end
subplot(3,1,1);
stem(x);
subplot(3,1,2);
stem(h);
subplot(3,1,3);
stem(y);
RESULT:
CONCLUSION:
PRACTICAL 8
AIM: To produce musical notes using different frequencies.
CODE:
clc;
clear all;
close all;
f0=340;
a=f0*(2^(7/12));
d=f0;
fs=f0*(2^(4/12));
f=f0*(2^(3/12));
g=f0*(2^(5/12));
bf=f0*(2^(8/12));
c=f0*(2^(10/12));
e=f0*(2^(2/12));
d2=2*d;
ts=1/8192;
tfs=0:ts:0.3;
t=0:ts:0.4;
ta=0:ts:1;
s1=0*(0:ts:0.1);
s2=0*(0:ts:0.05);
s3=0:ts:1;
d1=sin(2*pi*d*t);
f1=sin(2*pi*f*t);
g1=sin(2*pi*g*t);
e1=sin(2*pi*e*t);
bf1=sin(2*pi*bf*t);
c1=sin(2*pi*c*t);
dl1=sin(2*pi*d2*s3);
dl2=sin(2*pi*d*s3);
fs1=sin(2*pi*tfs*fs);
a11=sin(2*pi*a*s3);
a1=sin(2*pi*a*t);
sound (y)
CONCLUSION:
PRACTICAL 9
AIM: To obtain the spectrum of signal.
CODE:
FUNCTION 1
function y=f(k,w0,x);
y=x* exp(-j*k*w0*x);
endfunction
FUNCTION 2
function integral=trap(ll,ul,k,w0)
sum=0;
incv=0.01;
for x= ll:incv:ul
sum=sum+2.*f(k,w0,x);
end for
sum=sum-f(k,w0,ul)-f(k,w0,ll);
integral= (sum*incv)/2;
endfunction
CODE
clc;
clear all;
close all;
k= -49:49;
T=input('time period');
ll=input('lower limit');
ul=input('upperer limit');
w0=(2*pi)/T;
area= trap(ll,ul,k,w0);
mag= abs(area);
ang= angle(area);
subplot(2,1,1)
stem(k,mag);
subplot(2,1,2)
stem(k,ang);
RESULT:
CONCLUSION:
PRACTICAL 10
Aim: To generate Fourier series from Trigonometric Fourier
Series Coefficients.
Code:
FUNCTION 1
function y=a0()
y=(1-exp(-2*pi))/(2*pi);
endfunction
FUNCTION 2
function y=a1(n)
y=2/(pi*pi*n*n)*(cos(n*pi)-1);
endfunction
FUNCTION 3
function y=bn(n)
y=(n/(pi*(1+n*n)))*(1-exp(-2*pi));
endfunction
CODE
clc;
clear all;
close all;
t=0:0.01:4*pi;
y=a0(t);
y1=0;
y2=0;
for i=1:100;
y1=y1+an(i)*cos(i*t);
y2=y2+bn(i)*sin(i*t);
endfor
subplot(4,1,1);
plot(t,y);
xlabel('t');
ylabel('x(t)');
subplot(4,1,2);
plot(t,y1);
xlabel('t');
ylabel('x(t)');
subplot(4,1,3);
plot(t,y2);
xlabel('t');
ylabel('x(t)');
subplot(4,1,4);
plot(t,y+y1+y2,t,exp(-t));
title('Original Signal');
xlabel('t');
ylabel('x(t)');
Result:
Conclusion: