Wednesday, January 30, 2019

FREQUENCY RESPONSE FUNCTION AND DISCRETE TIME FOURIER TRANSFORM (DTFT)


DIGITAL SIGNALING PROCESSING
FREQUENCY RESPONSE FUNCTION AND DISCRETE TIME FOURIER TRANSFORM (DTFT)
 THEORY:
 Frequency Response Function:-The frequency response function of a discrete time system is found by substituting  z = ejw in the pulse transfer function of the system.
Mathematically,

 The following example demonstrates how MATLAB computes and plots the frequency response function.
 Example 1: Sketch the normalized frequency response function of the system having the pulse transfer function

 Solution:
Write the following MATLAB code.
 num=[1 0.95];
den = [1 –1.8 0.81];
w =- pi : pi/255 : pi % frequency vector
h=freqz(num,den,w); % compute frequency response of the system.
h1 = abs(h); % magnitude response
 h2 = h1/(max(h1)); % normalization
 h3=20*log10(h2); % normalized magnitude shown in dB scale
plot(w,h3)

Discrete Time Fourier Transform (DTFT):
We have already studied the Discrete-Time Fourier Transform (DTFT) in the class. There are several ways to plot DTFT using MATLAB. Consider the following example:
Example 2: Plot the spectrum (DTFT) of the following system:




Solution:
Write the following MATLAB code.
k = 256; % frequency points
num=[ 0.008 -0.033 0.05 -0.033 0.008];
 den=[1 2.37 2.7 1.6 0.41];
w=0:pi/k:pi;
h=freqz(num,den,w);
subplot(221); plot(w/pi,real(h)),
grid ;
title('Real Part')
 xlabel('Normalized angular frequency')
ylabel('Amplitude')
 subplot(222)
plot(w/pi,imag(h));
grid
title('imaginary part')
xlabel('Normalized angular frequency')
 ylabel('Amplitude')
 subplot(223)
plot(w/pi,abs(h)),
grid
title('Magnitude Spectrum')
xlabel('Normalized angular frequency')
ylabel('Magnitude')
subplot(224)
plot(w/pi,angle(h));
grid title('Phase Spectrum')
xlabel('Normalized angular frequency')
ylabel('Phase, radians')

EXERCISE
 Repeat example1 for the following system:



num=[1 1];
den = [1 0.1 -0.2];
w =- pi : pi/255 : pi % frequency vector
h=freqz(num,den,w); % compute frequency response of the system.
h1 = abs(h); % magnitude response
 h2 = h1/(max(h1)); % normalization
 h3=20*log10(h2); % normalized magnitude shown in dB scale
plot(w,h3)



Sketch the magnitude and phase response of the system described by the system function.


k = 256; % frequency points
num=[1];
 den=[1 -0.8];
w=0:pi/k:pi;
h=freqz(num,den,w);
subplot(221); plot(w/pi,real(h)),
grid ;
title('Real Part')
 xlabel('Normalized angular frequency')
ylabel('Amplitude')
 subplot(222)
plot(w/pi,imag(h));
grid
title('imaginary part')
xlabel('Normalized angular frequency')
 ylabel('Amplitude')
 subplot(223)
plot(w/pi,abs(h)),
grid
title('Magnitude Spectrum')
xlabel('Normalized angular frequency')
ylabel('Magnitude')
subplot(224)
plot(w/pi,angle(h));
grid; title('Phase Spectrum')
xlabel('Normalized angular frequency')
ylabel('Phase, radians')



BASICS OPERATION OF IMAGE PROCESSING WITH MATLAB


DIGITAL SIGNAL PROCESSING
 BASICS OPERATION OF IMAGE PROCESSING WITH MATLAB
INTRODUCTION:
Digital image processing is transforming digital information representing images.
Image Processing Toolbox: The Image Processing Toolbox is a collection of functions that extend the capabilities of the MATLAB’s numeric computing environment. The toolbox supports a wide range of image processing operations.
MATLAB can import/export several image formats such as BMP (Microsoft Windows Bitmap), GIF (Graphics Interchange Files) and JPEG (Joint Photographic Experts Group) etc.
IMAGES IN MATLAB:
• Binary images: {0, 1}
• Intensity images: [0, 1] or uint8, double etc.
• RGB images: m × n × 3


Images and Matrices How to build a matrix (or image)
Example 1: Intensity Image:
row = 256;
col = 256;
img = zeros(row, col);
img(100:105, :) = 0.5;
img(:, 100:105) = 1;
figure;
imshow(img);


Example 2: Binary Image:
row = 256;
col = 256;
img = rand(row, col);
img = round(img);
figure;
imshow (img);
Basic commands of image processing

Imread
A = imread (filename, fmt) reads a gray scale or true color image named filename into A. If the file contains a gray scale intensity image, A is a two dimensional array. If the file contains a true color (RGB) image, A is a three dimensional (m by n by 3) array.
Note: when you read image you have to give the path of drive where image is placed along with image name and format.
A=imread('drive:/ folder_name/image_name')
Rotation:
B = imrotate (a,angle)
a is your image.
ANGLE is the angle (in degrees) you want to rotate your image in the counter clockwise direction.
Edge Tracing
b=rgb2gray (a); % convert to gray.
WE can only do edge tracing for gray images.
edge(b,'prewitt');
edge(b,'sobel');
edge(b,'sobel','vertical');
edge(b,'sobel','horizontal');

Scaling:
imresize Resizes the image.
B = imresize (A, M) returns an image that is M times the size of A. If M is between 0 and 1.0, B is smaller than A. If M is greater than 1.0, B is larger than A.
B = imresize (A, [MROWS MCOLS]) returns an image of size MROWS by MCOLS.
If the specified size does not produce the same aspect ratio as the input image has, the output image is distorted.
Note: you can check the size of image using command “size”.

Export image:
To display image we use the following command
imshow (a)
and to write an image we use command
 imwrite (a,'filename.format')
EXERCISE:
1. Generate the image that is reverse of example 1 (convert black area of image to white and white area to black).

row = 256;
col = 256;
img = ones(row, col);
img(100:105, :) = 0;
img(:, 100:105) = 0.5;
figure
imshow(img)



2. Read any image using “imread” and apply basic image processing commands studied and show the output image for each command.
READING IMAGE:
A=imread('logo.jpg')
figure
imshow(A)

ROTATE:
B=imrotate(A,60)
figure
imshow(B)


EDGE TRACING (Sobel):
C=rgb2gray(A)
D=edge(C,'sobel')
figure
imshow(D)

EDGE TRACING (Prewitt):
C=rgb2gray(A)
D=edge(C,'prewitt')
figure
imshow(D)


SCALING:
B=imresize(A,1/2)
figure
imshow(B)


3. Run the following code.
ima = zeros(100,100);
for  i = 1:100
for  j = 1:100
ima(i,j) = i+j;
end
end
imagesc(ima)



Tuesday, January 29, 2019

SAMPLING THEORM WITH MATLAB CODING


DIGITAL SIGNAL PROCESSING
SAMPLING THEORM WITH MATLAB CODING
THEORY:
In the field of digital signal processing, the sampling theorem is a fundamental bridge between continuous signals (analog domain) and discrete signals (digital domain).
The Nyquist sampling theorem provides a prescription for the nominal sampling interval required to avoid aliasing. It may be stated simply as follows
“The sampling frequency should be at least twice the highest frequency contained in the signal”
Or in mathematical terms:
fs ≥ 2f max
Where fs is the sampling frequency and fmax is the highest frequency contained in the signal.
MATLAB CODE:
clear
fmax=3;             %maximum frequency of signal
t=0:0.0001:1;
x=cos(2*pi*fmax*t);
subplot(221)
plot(t,x)
xlabel('time in seconds');
ylabel('x(t)');
title('original signal');
fs1=fmax;           %samplinf frequency = fmax
fs2=2*fmax;         %samplinf frequency = two time fmax
fs3=8*fmax;         %samplinf frequency = eight times fmax

%%% under sampled %%%%
t1=0:1/fs1:1
x1=cos(2*pi*fmax*t1);
subplot(222)
plot(t1,x1)
hold on
stem(t1,x1)
xlabel('time in seconds');
ylabel('x1');
title('under sampled');

%%% two times fmax %%%%
t2=0:1/fs2:1
x2=cos(2*pi*fmax*t2);
subplot(223)
plot(t2,x2)
hold on
stem (t2,x2)
xlabel('time in seconds');
ylabel('x2');
title('two time fmax');

%%% over sampled %%%%
t3=0:1/fs3:1
x3=cos(2*pi*fmax*t3);
subplot(224)
plot(t3,x3)
hold on
stem(t3,x3)
xlabel('time in seconds');
ylabel('x3');
title('over sampled');
RESULTS:
See picture NAMED RESULT 1
EXERCISE:
1. Generate the following signals and plot the signal by taking the samples below the Nyquist rate, exactly equal to Nyquist rate and six times the Nyquist rate.
(a)   X(t) = 5 cos 15 π t + 10 sin 25 π t
MATLAB CODE:
close
clear
fmax=7;         %maximum frequency of signal
t=0:0.0001:1;
x1=5*cos(15*pi*t);
x2=10*sin(25*pi*t);
x=x1+x2;
subplot(221)
plot(t,x)
xlabel('time in seconds');
ylabel('x(t)');
title('original signal');
fs1=fmax/2;           %sampling frequency = fmax/2 (below)
fs2=fmax;         %sampling frequency = fmax (exactly equal)
fs3=6*fmax;         %sampling frequency = six times fmax (6*times)
%%% under sampled %%%%
t1=0:1/fs1:1
x11=5*cos(15*pi*t1)+10*sin(25*pi*t1);
subplot(222)
plot(t1,x11)
hold on
stem(t1,x11)
xlabel('time in seconds');
ylabel('x11');
title('under sampled');

%%% equal to fmax %%%%
t2=0:1/fs2:1
x22=5*cos(15*pi*t2)+10*sin(25*pi*t2);
subplot(223)
plot(t2,x22)
hold on
stem (t2,x22)
xlabel('time in seconds');
ylabel('x22');
title('equals to fmax');

%%% 6 times sampled %%%%
t3=0:1/fs3:1
x33=5*cos(15*pi*t3)+10*sin(25*pi*t3);
subplot(224)
plot(t3,x33)
hold on
stem(t3,x33)
xlabel('time in seconds');
ylabel('x33');
title('6 times sampled');

RESULTS
 See picture NAMED RESULT 2
(b)   Y(t )= 25 sin 20 π t + 50 sin 16 π t + 30 cos 28π t
MATLAB CODE:
close
clear
fmax=7;         %maximum frequency of signal
t=0:0.0001:1;
y1=25*sin(20*pi*t);
y2=50*sin(16*pi*t);
y3=30*cos(28*pi*t);
y=y1+y2+y3;
subplot(221)
plot(t,y)
xlabel('time in seconds');
ylabel('y(t)');
title('original signal');
fs1=fmax/2;           %sampling frequency = fmax/2 (below)
fs2=fmax;         %sampling frequency = fmax (exactly equal)
fs3=6*fmax;         %sampling frequency = six times fmax (6*times)
%%% under sampled %%%%
t1=0:1/fs1:1
y11=25*sin(20*pi*t1)+50*sin(16*pi*t1)+30*cos(28*pi*t1);
subplot(222)
plot(t1,y11)
hold on
stem(t1,y11)
xlabel('time in seconds');
ylabel('y11');
title('under sampled');

%%% equal to fmax %%%%
t2=0:1/fs2:1
y22=25*sin(20*pi*t2)+50*sin(16*pi*t2)+30*cos(28*pi*t2);
subplot(223)
plot(t2,y22)
hold on
stem (t2,y22)
xlabel('time in seconds');
ylabel('y22');
title('equals to fmax');

%%% 6 times sampled %%%%
t3=0:1/fs3:1
y33=25*sin(20*pi*t3)+50*sin(16*pi*t3)+30*cos(28*pi*t3);
subplot(224)
plot(t3,y33)
hold on
stem(t3,y33)
xlabel('time in seconds');
ylabel('y33');
title('6 times sampled');
RESULTS:
See picture NAMED RESULT 3