Showing posts with label Matlab. Show all posts
Showing posts with label Matlab. Show all posts

Wednesday, November 18, 2009

Program to design Highpass FIR Filter using Hamming Windows

CODE:
clc
clear all
close all
sn = 0;
f=input('Enter The Sampling Frequency Where f = 0.8 or 0.6 or 0.4 or 0.2 := ');
n=input('Enter The Order Of Filter Where n = 16 or 32 or 64 or 128 or 256 := ');
disp(' For Hamming Window ');
for i=1:256
sn(i) = ((4*sin(2*pi*(f)*i)) + ((4/3)*sin(2*pi*3*(f)*i))+((4/5)*sin(2*pi*5*(f)*i))+((4/7)*sin(2*pi*7*(f)*i)));
end
subplot(3,2,1);
plot(sn);grid;
axis([0 150 -10 10]);

%Create a filter and use sn as input
wn = 0.2 * pi; %Cutoff frequency
win=hanning(n+1);
b = fir1(n,wn,'high',win);

%Creating FIR HP filter with hamming window
hs = filter(b,1,sn);

%Plot the output
subplot(3,2,2);
plot(hs);grid;
axis([0 150 -10 10]);
title('Output of the FIR HP filter');

%Plot the magnitude response of the filter
subplot(3,2,3);
[H,F] = freqz(b,1,256);





% returns the frequency response vector H and the corresponding angular frequency vector F
plot(F,20 * log10(abs(H)));grid;
xlabel('Normalized F (rad/sample)');
ylabel('Magnitude (dB)');
title('Magnitude Response');

%Plot the phase response of the filter
subplot(3,2,4);
plot(F,unwrap(angle(H))*180/pi); grid;
xlabel('Normalized F (rad/sample)');
ylabel('Phase (degrees)');
title('Phase Response');

%Plot the filter
subplot(3,2,5);
plot(b);grid;
xlabel('Filter using hamming window');
fvtool(b);

Program to design Lowpass FIR Filter using Hamming Windows

CODE

clc
clear all
close all
sn = 0;
f=input('Enter The Sampling Frequency Where f = 0.8 or 0.6 or 0.4 or 0.2 := ');
n=input('Enter The Order Of Filter Where n = 16 or 32 or 64 or 128 or 256 := ');
disp(' For Hamming Window ‘);
windowtype=input('Enter The Type Of Window: =');
for i=1:256
sn(i) = ((4*sin(2*pi*(f)*i)) + ((4/3)*sin(2*pi*3*(f)*i))+((4/5)*sin(2*pi*5*(f)*i))+((4/7)*sin(2*pi*7*(f)*i)));
end
subplot(3,2,1);
plot(sn);grid;
axis([0 150 -10 10]);

%Create a filter and use sn as input
wn = 0.2 * pi; %Cutoff frequency
win=hamming(n+1);
b = fir1(n,wn,win);
%Creating FIR LP filter with hamming window
hs = filter(b,1,sn);

%Plot the output
subplot(3,2,2);
plot(hs);grid;
axis([0 150 -10 10]);
title('Output of the FIR LP filter');

%Plot the magnitude response of the filter
subplot(3,2,3);
[H,F] = freqz(b,1,256);




% returns the frequency response vector H and the corresponding angular frequency vector F

plot(F,20 * log10(abs(H)));grid;
xlabel('Normalized F (rad/sample)');
ylabel('Magnitude (dB)');
title('Magnitude Response');


%Plot the phase response of the filter
subplot(3,2,4);
plot(F,unwrap(angle(H))*180/pi); grid;
xlabel('Normalized F (rad/sample)');
ylabel('Phase (degrees)');
title('Phase Response');

%Plot the filter
subplot(3,2,5);
plot(hs);grid;
xlabel('Filter using hamming window');
fvtool(b);

Program to perform 2-D Cross Correlation & Auto Correlation

CODE


clc
clear all
close all
x=[1 2;3 4]
h=[5 6;7 8]
y=xcorr2(x,h);
disp('Cross Correlation');
disp(y);
y1=xcorr2(x);
disp('Auto Correlation');
disp(y1);

To perform Circular Convolution

CODE


%x(n)--> Input Sequence% Given-->[1 2 4]
%h(n)--> Impulse Response% Given-->[1 2]
clc
clear all
close all

x=input('Enter x(n) : = ');
h=input('Enter h(n) := ');

N1=length(x);
N2=length(h);
N=max(N1,N2);

y=conv(x,h); %stores value according to linear conv.
ly=length(y);

for i=1:1:N
if(N+i<=ly)
r(i)=y(i)+y(N+i);
else
r(i)=y(i);
end
end
disp(r)

Write a program to perform 2-Dimensional Linear Convolution

CODE:


clc
clear all
close all
x=[1 2;3 4];
h=[5 6;7 8];

y=conv2(x,h);
disp(y)
subplot(3,1,1);plot(x);title('Input Sequence');
subplot(3,1,2);plot(h);title('Impulse Response');
subplot(3,1,3);plot(y);title('Output Sequence');

Write a program to perform 1-Dimensional Linear Convolution.

CODE


%x(n)--> Input Sequence% Given-->[1 1 1 1 1]
%h(n)--> Impulse Response% Given-->[1 2 3 4 5 6 7 8]
clc
clear all
close all
x=[1 1 1 1 1];
h=[1 2 3 4 5 6 7 8];
y=conv(x,h);


figure;
subplot(3,1,1);stem(x);
xlabel('n---->');
ylabel('Amplitude---->');
title('Input Sequence');


subplot(3,1,2);stem(h);
xlabel('n---->');
ylabel('Amplitude---->');
title('Impulse Response');


subplot(3,1,3);stem(y);
xlabel('n---->');
ylabel('Amplitude---->');
title('Output Sequence');

Program to read a .wav file, plot the graph & play the file.

CODE



InputFilename = 'chord.wav';
[inspeech, Fs, bits] = wavread(InputFilename);
figure(1);
subplot(3,1,1);
plot(inspeech);
grid;
soundsc(inspeech, Fs);

Program for generating basic functions. Unit Imupulse

CODE

clc
clear all
close all
n=6;


%To Generate Unit Impulse Siganl%
x=-2:1:2;
y=[zeros(1,2),ones(1),zeros(1,2)];
subplot(2,2,1);stem(x,y);
ylabel('Amplitude---->');
xlabel('n---->');
title('Unit Impulse Signal');


%To Generate Unit Step Signal%
t=0:1:n-1;
y1=ones(1,n);
subplot(2,2,2);stem(t,y1);
ylabel('Amplitude---->');
xlabel('n---->');
title('Unit Step Signal');


%To Generate Ramp Signal%
t1=0:1:n;
subplot(2,2,3);stem(t1,t1);
ylabel('Amplitude---->');
xlabel('n---->');
title('Ramp Sequence');


%To Generate Exponentional Sequence%
y2=exp(-1*t1);
subplot(2,2,4);stem(t1,y2);
ylabel('Amplitude---->');
xlabel('n---->');
title('Exponentional Sequence');



%To Generate Sine Sequence%
pause
t2=0:0.01:pi;
y3=sin(2*pi*t2);figure(2);
subplot(2,1,1);plot(t2,y3);
ylabel('Amplitude---->');
xlabel('n---->');
title('Sine Sequence');

%To Generate Cose Sequence%
t2=0:0.01:pi;
y3=cos(2*pi*t2);
subplot(2,1,2);plot(t2,y3);
ylabel('Amplitude---->');
xlabel('n---->');
title('Cose Sequence');

Low pass Median filter on image with salt and pepper noise

CODE

clear all
clc

a = imread('cameraman.tif');
p = imnoise(a,'salt & pepper',0.02'); %Adding noise

q = double(p);

[row col] = size(q);

for i=2:1:row-1
for j=2:1:col-1
%To make a 3x3 mask into 1x9 mask
q1=[ q(i-1,j-1) q(i-1,j) q(i-1,j+1) q(i,j-1) q(i,j) q(i,j+1) q(i+1,j-1) q(i+1,j)
q(i+1,j+1) ];
q2=sort(q1);
median=q2(5); %The fifth value is the median
q3(i,j)=median;
end
end

figure(1)
imshow(uint8(p))
title('Image after ''Salt & Pepper'' Noise')

figure(2)
imshow(uint8(q3))
title('Image after cleaning the ''salt & pepper'' noise using ''Low pass Median Filtering'' technique')

High Pass Filter in Spatial Domain

CODE

% High pass filter done on an image

clear all
clc

a = imread('cameraman.tif');
p = double(a);

w=[-1 -1 -1;-1 8 -1;-1 -1 -1] %Creating the high pass average mask

[row col]=size(p)

for i=2:1:row-1
for j=2:1:col-1
p1(i,j) =[ (w(1) * p(i-1,j-1)) + (w(2) * p(i-1,j)) + (w(3) * p(i-1,j+1)) + (w(4) *
p(i,j-1)) + (w(5) * p(i,j)) + (w(6) * p(i,j+1)) + (w(7) * p(i+1,j-1)) + (w(8) * p(i+1,j)) + (w(9) * p(i+1,j+1)) ];
end
end

figure(1)
imshow(uint8(p))
title('Original Image');

figure(2)
imshow(uint8(p1))
title('Image after ''High Pass'' Filtering');

Low Pass Filter in Spatial domain

CODE

% Low pass Average filter used on an image with Gaussian noise

clear all
clc

a = imread('cameraman.tif');
p = imnoise(a,'gaussian'); %Adding noise
q = double(p);
[row col]=size(q)

w = [1 1 1;1 1 1;1 1 1]/9 %Creating the low pass average mask

for i=2:1:row-1
for j=2:1:col-1
q1(i,j) = [ (w(1) * q(i-1,j-1)) + (w(2) * q(i-1,j)) + (w(3) * q(i-1,j+1)) + (w(4)
* q(i,j-1)) + (w(5) * q(i,j)) + (w(6) * q(i,j+1)) + (w(7) * q(i+1,j-1))
+ (w(8) * q(i+1,j)) + (w(9) * q(i+1,j+1)) ];
end
end

figure(1)
imshow(uint8(q))
title('Original Image with Gaussian Noise');

figure(2)
imshow(uint8(q1))
title('Image after using Low Pass Average Filter on Gaussian Noised image');

Write a program to demonstrate Histogram Equalization

CODE


% Histogram Equalization%

f = imread('tire.tif');
g = histeq(f);
title('Histogram');

subplot(2,2,1);
imshow(f);
title('Input Image');

subplot(2,2,2);
imhist(f);
title('Histogram');

subplot(2,2,3);
imshow(g);
title('Image after Histogram Equalization');

subplot(2,2,4);
imhist(g);
title('Histogram');

Write a program to demonstrate Dynamic Range Compression

% Dynamic Range Compression

clear all ;
clc;
aa=imread('saturn.tif');
a=double(aa);
[row,col]=size(a);
for x=1:1:row
for y=1:1:col
c(x,y)=a(x,y)*((-1)^(x+y));%%Needed to center transform%%
end
end
d=abs(fft2(c));
d_log=log(1+d);
%%Plotting
figure(1);
colormap(gray);
imagesc(d);
figure(2);
colormap(gray);
imagesc(d_log);

Intensity level slicing with Background and without Background

Note:-Write this code in Matlab

CODE

%Intensity level slicing with background%

clear all ;
clc;
p=imread('lily.tif');
z=double(p);
[row,col]=size(z);
for i=1:1:row
for j=1:1:col
if((z(i,j)>50)) && (z(i,j)<150)
z(i,j)=255;
else
z(i,j)=p(i,j);
end
end
end
figure(1); %-----------Original Image-------------%
imshow(p);
figure(2); %-----------Gray Level Slicing With Background-------------%
imshow(uint8(z));



Intensity Level slicing without background


CODE

%Intensity level slicing without background%

clear all;
clc;
p=imread('lily.tif');
z=double(p);
[row,col]=size(z)
for i=1:1:row
for j=1:1:col
if((z(i,j)>50)) && (z(i,j)<150)
z(i,j)=255;
else
z(i,j)=0;
end
end
end
figure(1); %-----------Original Image-------------%
imshow(p);
figure(2); %-----------Gray Level Slicing Without Background-------------%
imshow(uint8(z));

Tuesday, November 17, 2009

To perform thesholding of a monochrome / 16 / 24 bit bitmap / JPEG / TIFF / GIF / Color image

Note:-Write this code in Matlab

CODE

% thresholding
clf;
clc;
clear all;
close all;
i=imread('camera.bmp');
subplot(1,2,1);
imshow(i);
title('original image');
r1=100;
[r c]=size(i);
for x=1:c
for y=1:r
if (i(y,x) i(y,x)=0;
else
i(y,x)=255;
end
end
end

subplot(1,2,2)
imshow(i);
title('thresholding image');

To perform Contrast Stretching of a monochrome / 16 /24 bit bitmap / JPEG / TIFF / GIF / Color image

Note:-Write this code in Matlab


CODE

%contrast stretching of image
clf;
clc;
clear all;
close all;
i=imread('camera.bmp');
subplot(1,2,1);
imshow(i);
title('original image');
i=double(i);
r1=100;
r2=150;
s1=175;
s2=250;
MAX=255;
[r c]=size(i);
for x=1:c
for y=1:r
if(i(y,x) < r1)
i(y,x)=(i(y,x)*(s1/r1));
if(i(y,x) < r2)
i(y,x)= s1 + (i(y,x)-r1)*((s2-s1)/(r2-r1));
else
i(y,x)= s2 +((i(y,x)-r2)*((MAX-s2)/(MAX-r2)));
end
end
end
end
i=uint8(i);
subplot(1,2,2)
imshow(i);
title('Contrast stretching image');

To perform Negation of a monochrome / 16 / 24 bit bitmap / JPEG / TIFF / GIF / Color image

Note:- write this code in Matlab





CODE

%To create negative image
clf;
clf;
clear all;
close all;
i=imread('camera.bmp');
subplot(1,2,1);
imshow(i);
title('original image');
i=double(i);
[r c]=size(i);
for x=1:c
for y=1:r
i(y,x)=255-i(y,x);
end
end
i=uint8(i);
subplot(1,2,2)
imshow(i);
title('Negative image');

To perform Negation of a monochrome / 16 / 24 bit bitmap / JPEG / TIFF / GIF / Color image

This code is in MATLAB



CODE

%To create negative image
clf;
clf;
clear all;
close all;
i=imread('camera.bmp');
subplot(1,2,1);
imshow(i);
title('original image');
i=double(i);
[r c]=size(i);
for x=1:c
for y=1:r
i(y,x)=255-i(y,x);
end
end
i=uint8(i);
subplot(1,2,2)
imshow(i);
title('Negative image');