Speech Recognition in MATLAB using Correlation

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a tutorial on Speech Recognition in MATLAB using Correlation. Speech recognition is used in almost every security project where you need to speak and tell your password to a computer and is also used for automation. For example, I want to turn my AC on or off using voice commands then I have to use Speech Recognition. I have to make the system recognize that whether I am saying ON or OFF. In short, speech recognition plays a vital role in voice control projects. In today's post, I am gonna show you How to do Speech Recognition in Matlab and the technique I have used in this project is known as cross correlation. You should also have a look at Eye Ball Detection in MATLAB.

Correlation is normally used in signal processing, where you need to compare two signals and need to find the similarity between them. It is also known as the dot product of those two signals. Correlation has many uses and you can read more about it on its Wiki Page. Correlation is also used for pattern recognition like you want to find some pattern in the signal then you can use Correlation. Anyways, in our project, we are using correlation to find similarities between our stored signals and the testing signal. So, let's get started with Speech Recognition in MATLAB using Correlation.

Speech Recognition in MATLAB using Correlation

  • First of all, download this complete project by clicking the below button:
  • Now in this package, you will find nine audio wav files.
  • Five of them are the recorded sounds that are already feed in MATLAB.
  • Two are test files that will be recognized by the code.
  • The remaining two are success and failure files which will run if you got the recognition or not.
  • Let me explain the code a bit. First of all, what we need to do is to upload the first five training audio files in the software so and then we need to test these fives files with the test files and we need to check which one is a maximum match.
  • Here's the complete code:
function speechrecognition(filename)
voice=wavread(filename);
x=voice;
x=x';
x=x(1,:);
x=x';
y1=wavread('one.wav');
y1=y1';
y1=y1(1,:);
y1=y1';
z1=xcorr(x,y1);
m1=max(z1);
l1=length(z1);
t1=-((l1-1)/2):1:((l1-1)/2);
t1=t1';
%subplot(3,2,1);
plot(t1,z1);
y2=wavread('two.wav');
y2=y2';
y2=y2(1,:);
y2=y2';
z2=xcorr(x,y2);
m2=max(z2);
l2=length(z2);
t2=-((l2-1)/2):1:((l2-1)/2);
t2=t2';
%subplot(3,2,2);
figure
plot(t2,z2);
y3=wavread('three.wav');
y3=y3';
y3=y3(1,:);
y3=y3';
z3=xcorr(x,y3);
m3=max(z3);
l3=length(z3);
t3=-((l3-1)/2):1:((l3-1)/2);
t3=t3';
%subplot(3,2,3);
figure
plot(t3,z3);
y4=wavread('four.wav');
y4=y4';
y4=y4(1,:);
y4=y4';
z4=xcorr(x,y4);
m4=max(z4);
l4=length(z4);
t4=-((l4-1)/2):1:((l4-1)/2);
t4=t4';
%subplot(3,2,4);
figure
plot(t4,z4);
y5=wavread('five.wav');
y5=y5';
y5=y5(1,:);
y5=y5';
z5=xcorr(x,y5);
m5=max(z5);
l5=length(z5);
t5=-((l5-1)/2):1:((l5-1)/2);
t5=t5';
%subplot(3,2,5);
figure
plot(t5,z5);
m6=300;
a=[m1 m2 m3 m4 m5 m6];
m=max(a);
h=wavread('allow.wav');
if m<=m1
    soundsc(wavread('one.wav'),50000)
        soundsc(h,50000)
elseif m<=m2
    soundsc(wavread('two.wav'),50000)
        soundsc(h,50000)
elseif m<=m3
    soundsc(wavread('three.wav'),50000)
        soundsc(h,50000)
elseif m<=m4
    soundsc(wavread('four.wav'),50000)
        soundsc(h,50000)
elseif m<m5
    soundsc(wavread('five.wav'),50000)
        soundsc(h,50000)
else
   {soundsc(wavread('denied.wav'),50000)}
   
end
  • Now if you read the code from start then you can see, first of all, I uploaded the test file which I want to compare with my samples.
  • After that, I uploaded all 5 samples and also get their correlation with the test sample.
  • Finally, in the end, I compared the results and on the basis of it I figured out which one is the correct speech file.
  • You will also get spectrum graphs of your sound files as shown in the below figure:
  • It was quite an easy project and I have done it within half an hour and I hope you guys will understand.
  • If you got any problems then ask in the comments and I will resolve them.
  • Here's the complete video demonstration for this project and I hope it's going to help you out in understanding it.
That's all for today, and I think you have understood How to do Speech Recognition in MATLAB using Correlation. Will meet you guys in the next tutorial soon. Till then take care !!! :)

Image Zooming with Bilinear Interpolation in MATLAB

Hello friends, hope you all are fine and having fun with your lives. Today, I am going to share a simple tutorial on Image zooming with bilinear Interpolation in MATLAB. We have seen many software in which there's an option of zooming an image. For example, if you have used paint or photoshop then you have seen that you can zoom your image quite easily by clicking a button. Today's we are gonna do the same thing but in MATLAB and we will have a look at the code behind this feature.

Now, when we are zooming some image then in fact we are increasing the pixels of that image and in order to do that we have to fill those extra pixels with the color of their neighbor pixel. This thing is know as interpolation. There are many different techniques for interpolation and the one we are gonna use for this tutorial is known as Bilinear Interpolation. Bilinear interpolation is simple type of linear interpolation in which we simply apply interpolation formula on both the x and y axis. So, let's have a brief overview of Bilinear Interpolation first and then we will move on to MATLAB implementation.

What is Bilinear Interpolation ?

  • As I explained earlier, Bilinear Interpolation is a simple interpolation technique in which we fill the gaps between pixels using the neighbor pixels.
  • For example, we have an unknown pixel in between four pixels, and let's say the unknown pixel is f(x,y) and it is surrounded by four pixels which are:
    1. Q11 = (x1y1).
    2. Q12 = (x1y2).
    3. Q21 = (x2y1).
    4. Q22 = (x2y2).
  • All these four neighbor pixels are known , now by using Bilinear Interpolation we can find the values of this unknown pixel.
  • Now, first of all, we will move in the x direction only.
  • The formula used for Bilinear Interpolation for x factor is:
  • Now after calculating these x formulas, now we will move in y direction and the formulas are:
  • Now using these formulas we can quite easily find our unknown pixel f(x,y) using Bilinear interpolation. These formulas are taken from Wiki Page of Bilinear Interpolation and you can read more details about it there.
  • Now we have seen the Bilinear Interpolation so now let's move and see How to do Image Zooming using this Bilinear interpolation in MATLAB.

Image Zooming with Bilinear Interpolation in MATLAB

  • You can download the complete code by clicking the below button but also read the post, as I have explained this code in this remaining post.

Download MATLAB Code for Image Zooming

  • In order to do image zooming with Bilinear Interpolation in MATLAB, first of all, what you need to do is to read an image file, which I have done using this simple formula:
im0=imread('TEP.jpg');
im=cast(im0,'int16');
imshow(cast(im,'uint8'));
[h,v,d]=size(im);
  • So, in this above code, what we did is , we simply read the image file which I have named as TEP.jpg
  • After that I have converted this image file into int16 and then to uint8 and finally I get the size of this image file using size command.
  • After that I have applied a simple algorithm and have zoomed my image using below code:
for i=1:h
    for j=1:v
      im1(1+(i-1)*fac,1+(j-1)*fac,:)=im(i,j,:); 
    end
       imshow(cast(im1,'uint8')); 
end
  • Now in the above loop what we have done is we simply enhanced our image and named it as im1, I have used a variable fac, which is factor, its user defined like if you want to zoom image by 2 then factor will be 2.
  • Now we have enhanced our image, next thing we need to do is to apply the bilinear Interpolation on this complete image and we will get the result.
  • Here's the complete code for Image Zooming with Bilinear Interpolation in MATLAB.
function bilinear_zoom(fac)

im0=imread('TEP.jpg');
im=cast(im0,'int16'); 
imshow(cast(im,'uint8'));
[h,v,d]=size(im);

for i=1:h
    for j=1:v
      im1(1+(i-1)*fac,1+(j-1)*fac,:)=im(i,j,:); 
    end
       imshow(cast(im1,'uint8')); 
end

%bilinear interpolation
for i=1:1+(h-2)*fac     %row number
    for j=1:1+(v-2)*fac %column number
    
       if ((rem(i-1,fac)==0) && (rem(j-1,fac)==0)) 
       else  
           h00=im1( ceil(i/fac)*fac-fac+1,ceil(j/fac)*fac-fac+1,:); 
           h10=im1( ceil(i/fac)*fac-fac+1+fac,ceil(j/fac)*fac-fac+1,:);
           h01=im1( ceil(i/fac)*fac-fac+1,ceil(j/fac)*fac-fac+1+fac,:);
           h11=im1( ceil(i/fac)*fac-fac+1+fac,ceil(j/fac)*fac-fac+1+fac,:);
           
           x=rem(i-1,fac); %coordinates of calculating pixel.
           y=rem(j-1,fac);  
          
           dx=x/fac; %localizeing the  pixel being calculated to the nearest four know pixels.
           dy=y/fac;
          
           b1=h00;    %constants of bilinear interpolation.
           b2=h10-h00;
           b3=h01-h00;
           b4=h00-h10-h01+h11;           
           im1(i,j,:)=b1+b2*dx+b3*dy+b4*dx*dy; %equation of bilinear interpolation.
         end
        end
  imshow(cast(im1,'uint8'));
end

imshow(cast(im1,'uint8'));
imwrite(cast(im1,'uint8'),'zoomed_pic.jpg');
  • Now if you check in the Bilinear Interpolation code, we have applied the same equations which we have discussed in the above section.
  • The Image I have used for this code is as follows:
  • Now in MATLAB window, I have given this command bilinear_zoom(2) , where fac = 2, so I am increasing my image by factor 2. So it will be zoomed by 2 times.
  • Its the image from my 555 Timer post but as I have posted it recently so this image was on my desktop thats why I used it. :P
  • The result obtained is as follows:
  •  Now you can see the image has been zoomed and is now looking big and because of Bilinear Interpolation in MATLAB we haven't lost the in between pixels.
  • Here's a video for this tutorial, which will give you better idea of How to do Image Zooming with Bilinear Interpolation in MATLAB.
That's all for today, I hope you have enjoyed this post. Will meet you guys in the next tutorials soon. Till then take care. :)
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir