Auto AdSense

Sunday, 21 September 2014

MATLAB code for IIR Chebyshev Filter using Impulse Invariant Transform

clc;
clear all;
close all;
ap=20;
as=60;
fp=1000;
fs=3000;
fst=8000;
t=(1/fst);
wp=2*pi*fp;
ws=2*pi*fs;
w1=(2/t)*tan(wp/(2*fst));
w2=(2/t)*tan(ws/(2*fst));
[n,wn]=cheb1ord(w1,w2,ap,as,'s');
[b,a]=cheby1(n,ap,w1,'s');
[bz,az]=impinvar(b,a,fst);
freqz(bz,az,500);

MATLAB code for IIR Chebyshev filter using Bilinear Transform

clc;
clear all;
close all;
ap=20;
as=60;
fp=1000;
fs=3000;
fst=8000;
t=(1/fst);
wp=2*pi*fp;
ws=2*pi*fs;
w1=(2/t)*tan(wp/(2*fst));
w2=(2/t)*tan(ws/(2*fst));
[n,wn]=cheb1ord(w1,w2,ap,as,'s');
[b,a]=cheby1(n,ap,w1,'s');
[bz,az]=bilinear(b,a,fst);
freqz(bz,az,500);

MATLAB code for IIR Butterworth Filter using Impulse Invarient Transform

clc;
clear all;
close all;
Ap=2;
As=40;
fp=100;
fs=900;
fst=2000;
wp=2*pi*fp;
ws=2*pi*fs;
[n,wn]=buttord(wp,ws,Ap,As,'s');
[b,a]=butter(n,wn,'s');
[bz,az]=impinvar(b,a,fst);
freqz(bz,az,1000);

MATLAB code for IIR Butterworth Filter using Bilinear Transform

clc;
clear all;
close all;
Ap=2;
As=40;
fp=100;
fs=900;
fst=2000;
t=(1/fst);
wp=2*pi*fp;
ws=2*pi*fs;
w1=(2/t)*tan(wp/(2*fst));
w2=(2/t)*tan(ws/(2*fst));
[n,wn]=buttord(w1,w2,Ap,As,'s');
[b,a]=butter(n,wn,'s');
[bz,az]=bilinear(b,a,fst);
freqz(bz,az,fst);

Friday, 19 September 2014

MATLAB code for N-Point DIF FFT algorithm

clc;
clear all;
close all;
X=input('Enter the sequence : ');
N=input('Enter the Point    : ');
n=length(X);
x=[X zeros(1,N-n)];
M=log2(N);
for m=1:M
    d=2^(M-m+1);
    for l=1:d:(N-d+1)
        for k=0:(d/2)-1
            w=exp(-1i*2*pi*k/d);
            z1=x(l+k);
            z2=x(l+k+d/2);
            x(l+k)=z1+z2;
            x(l+k+d/2)=(z1-z2)*w;
        end
    end
end

y=bitrevorder(x);
disp(y)
subplot(3,1,1)
stem(abs(X));
title('Input Sequence');
subplot(3,1,2)
stem(abs(y));
title('Magnitude Response');
subplot(3,1,3)
stem(angle(y));
title('Phase Response');

MATLAB code for N-Point DIT FFT algorithm

clc;
clear all;
close all;
x=input('Enter the sequence : ');
N=input('Enter the Point    : ');
n=length(x);
x=[x zeros(1,N-n)];
y=bitrevorder(x);
M=log2(N);
for m=1:M
    d=2^m;
    for l=1:d:N-d+1
        for k=0:(d/2)-1
            w=exp(-1i*2*pi*k/d);
            z1=y(l+k);
            z2=y(l+k+d/2);
            y(l+k)=z1+w*z2;
            y(l+k+d/2)=z1-w*z2;
        end
    end
end
disp(y);

subplot(3,1,1)
stem(abs(x));
title('Input Sequence');
subplot(3,1,2)
stem(abs(y));
title('Magnitude Response');
subplot(3,1,3)
stem(angle(y));
title('Phase Response');

MATLAB code for Circular Convolution algorithm

clc;
clear all;
close all;
x=input('Enter the first Sequence');
h=input('Enter the second Sequence');
l=length(x);
m=length(h);
n=max(l,m);
x1=[x zeros(1,n-m)];
h1=[h zeros(1,n-l)];
for i=1:n
    sum=0;
    for k=1:n
        sum=sum+x1(k)*h1(mod(i-k,n)+1);
    end
    y(i)=sum;
end
disp(y)
p=cconv(x,h,n)
subplot(3,1,1)
stem(x1);
xlabel('time');ylabel('amplitude');title('First Sequence');
subplot(3,1,2)
stem(h1);
xlabel('time');ylabel('amplitude');title('Second Sequence');
subplot(3,1,3)
stem(y);
xlabel('time');ylabel('amplitude');title('Convoluted Sequence');

Wednesday, 17 September 2014

C program of Multiplication table of n upto m

/*Multiplication table of n upto m*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,I,m;
clrscr();
printf("enter n and m \n");
scanf("%d %d",&n,&m);
for(i=1;i<=m;i++)
printf("%d * %d = %d\n",n,i,n*i);
getch();
}

C Program to convert Decimal number to Binary

/*Decimal to binary conversion */


#include<stdio.h>

int main(){

    long int decimalNumber,remainder,quotient;

    int binaryNumber[100],i=1,j;

    printf("Enter any decimal number: ");

    scanf("%ld",&decimalNumber);

    quotient = decimalNumber;


    while(quotient!=0){

         binaryNumber[i++]= quotient % 2;

         quotient = quotient / 2;

    }


    printf("Equivalent binary value of decimal number %d: ",decimalNumber);

    for(j = i -1 ;j> 0;j--)

         printf("%d",binaryNumber[j]);


    return 0;

}

C Program to find the 2's Complement of the Binary number

/* 2’s complement of a number is obtained by scanning it from right to left and complementing all
the bits after the first appearance of a 1. Thus 2’s complement of 11100 is 00100. */

#include <stdio.h>
#include<conio.h>

void complement (char *a);
void main()
{
 char a[16];
 int i;
 clrscr();
 printf("Enter the binary number");
 gets(a);
 for(i=0;a[i]!='\0'; i++)
 {
  if (a[i]!='0' && a[i]!='1')
  {
   printf("The number entered is not a binary number. Enter the correct number");
   exit(0);
  }
 }
complement(a);
getch();
}
void complement (char *a)
{
 int l, i, c=0;
 char b[16];
 l=strlen(a);
 for (i=l-1; i>=0; i--)
 {
  if (a[i]=='0')
  b[i]='1';
  else
  b[i]='0';
 }
 for(i=l-1; i>=0; i--)
 {
 if(i==l-1)
 {
  if (b[i]=='0')
  b[i]='1';
  else
  {
   b[i]='0';
   c=1;
  }
 }
 else
 {
  if(c==1 && b[i]=='0')
  {
   b[i]='1';
   c=0;
  }
 else if (c==1 && b[i]=='1')
 {
  b[i]='0';
  c=1;
 }
}
}
b[l]='\0';
printf("The 2's complement is %s", b);
}



Friday, 12 September 2014

C programs that use both recursive and non-recursive functions To find the factorial of a given integer



#include<stdio.h>
#include<conio.h>

unsigned int recr_factorial(int n);
unsigned int iter_factorial(int n);

void main()
{
  int n,i;
  long fact;
  clrscr();
  printf("Enter the number: ");
  scanf("%d",&n);

  if(n==0)
    printf("Factorial of 0 is 1\n");
  else
  {
    printf("Factorial of %d Using Recursive Function is %d\n",n,recr_factorial(n));
    printf("Factorial of %d Using Non-Recursive Function is %d\n",n,iter_factorial(n));
   }
   getch();
}

/* Recursive Function*/
unsigned int recr_factorial(int n) {
    return n>=1 ? n * recr_factorial(n-1) : 1;
}

/* Non-Recursive Function*/
unsigned int iter_factorial(int n) {
    int accu = 1;
    int i;
    for(i = 1; i <= n; i++) {
accu *= i;
    }
    return accu;
}

C programs that use both recursive and non-recursive functions To find the GCD (greatest common divisor) of two given integers



#include<stdio.h>
#include<conio.h>
#include<math.h>

unsigned int GcdRecursive(unsigned m, unsigned n);
unsigned int GcdNonRecursive(unsigned p,unsigned q);

int main(void)
{
  int a,b,iGcd;
  clrscr();

  printf("Enter the two numbers whose GCD is to be found: ");
  scanf("%d%d",&a,&b);

  printf("GCD of %d and %d Using Recursive Function is %d\n",a,b,GcdRecursive(a,b));
  printf("GCD of %d and %d Using Non-Recursive Function is %d\n",a,b,GcdNonRecursive(a,b));

  getch();
}

/* Recursive Function*/
unsigned int GcdRecursive(unsigned m, unsigned n)
{
 if(n>m)
        return GcdRecursive(n,m);
 if(n==0)
         return m;
 else
     return GcdRecursive(n,m%n);
}

/* Non-Recursive Function*/
unsigned int GcdNonRecursive(unsigned p,unsigned q)
{
 unsigned remainder;
 remainder = p-(p/q*q);

 if(remainder==0)
     return q;
 else
     GcdRecursive(q,remainder);
}

C PROGRAM TO EXCHANGE THE VALUES OF TWO INTEGERS USING POINTERS


#include <stdio.h>
void exchange (int *, int *); /* prototype */
void main()
{
   int x, y;
   x = 100;
    y = 200;
    printf("Before exchange : x = %d y = %d\n\n", x, y);
    exchange(&x,&y);/* call */
    printf("After exchange : x = %d y = %d\n\n", x, y);
}
exchange (int *a, int *b)
{
int t;
t = *a; /* Assign the value at address a to t */
*a = *b; /* put b into a */
*b = t; /* put t into b */
}

C program to count the number of character ,number of lines from a text file


#include<stdio.h>
#include<conio.h>

void main()
{
int noc=0,now=0,nol=0;
FILE *fw,*fr;
char fname[20],ch;
clrscr();

printf("\n enter the source file name");
gets(fname);

fr=fopen(fname,"r");

if(fr==NULL)
{
   printf("\n error \n");
    exit(0);
}

ch=fgetc(fr);

while(ch!=EOF)
{
       noc++;
    if(ch==' ');
      now++;
    if(ch=='\n')
{
nol++;
now++;
}
    ch=fgetc(fr);
}
fclose(fr);


printf("\n total no of character=%d",noc);
printf("\n total no of words=%d",now);
printf("\n total no of lines=%d",nol);

getch();

C program to copy one file to another

#include <stdio.h>
#include <conio.h>
#include <process.h>

void main(int argc, char *argv[])
{
 FILE *fs,*ft;
 char ch;
 clrscr();
 if(argc!=3)
 {
  puts("Invalid number of arguments.");
  exit(0);
  }
 fs = fopen(argv[1],"r");
 if(fs==NULL)
 {
 puts("Source file cannot be opened.");
 exit(0);
 }
 ft = fopen(argv[2],"w");
 if (ft==NULL)
 {
 puts("Target file cannot be opened.");
 fclose(fs);
 exit(0);
 }
 while(1)
 {
  ch=fgetc(fs);
  if (ch==EOF)
  break;
  else
  fputc(ch,ft);
  }
  fclose(fs);
  fclose(ft);
  getch();
}

C program for Merging of two files into a third file


#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
FILE *fs1, *fs2, *ft;

char ch;

if(argc != 4)
{
printf("error in specifying the files");
exit(0);
}

fs1 = fopen(argv[1],"r");
fs2 = fopen(argv[2],"r");

if( fs1 == NULL || fs2 == NULL )
{
printf("Error in files");

}
else
{
    ft = fopen(argv[3],"w");

  if( ft == NULL )
 {
   printf("Error ");

  }
else
{
while( ( ch = fgetc(fs1) ) != EOF )
fputc(ch,ft);

while( ( ch = fgetc(fs2) ) != EOF )
fputc(ch,ft);

printf("Two files were merged into %s file successfully.\n",file3);

fclose(fs1);
fclose(fs2);
fclose(ft);
}
}

return 0;
}.