Tuesday 8 November 2011

Revolution...

Hey visitors...
Please visit my new Blog...
Actually it is Wordpress...
http://prashantkulkarni17.wordpress.com/
Thank You... :D :)

Wednesday 25 May 2011

Difference Equation...


/******************************************************************************
 Find Output of a System described by given difference equation & initial conditions for given I/P sqeuence.(Solution of difference equation).Obtain Response for different systems by changing degree of difference equation(N) & coefficients & also for difference input sequence x(n).Observe the Response by considering System as FIR & IIR system.
******************************************************************************/
#include<iostream.h>
#include<conio.h>
/******************************************************************************/
void main()
{
int a[100],b[100],x[100],y[100],i,j,k,l,m,n,M,N,ein,einit,es;
clrscr();
for(i=0;i<=100;i++)
a[i]=0;
     F: ein=einit=es=0;
cout<<"\n How Many Terms of Y (M+1)? ==> ";
cin>>M;
cout<<"\n How Many Terms of X (N+1)? ==> ";
cin>>N;


cout<<"\n Enter Coefficients Terms of X ==>  ";
for(i=0;i<N;i++)
cin>>a[i];
    D: if(ein==1)
{
cout<<"\n How Many Terms of X ? ==> ";
cin>>N;
ein=0;
}
cout<<"\n Enter x(N) ==>  ";
for(i=0;i<N;i++)
cin>>x[i];
if(M>1)
goto B;
cout<<"\n Difference Equation is ==> { ";
for(i=0;i<100;i++)
{
y[i]=0;
for(j=0;j<N;j++)
if(i-j>=0 && i-j<N)
y[i]=y[i]+a[j]*x[i-j];
cout<<" "<<y[i];
}
cout<<" }";
goto A;


    B:  if(ein==1)
goto G;


cout<<"\n Enter Coefficients Terms of Y ==>  ";
for(i=0;i<M;i++)
cin>>b[i];
    E: for(i=(-(M-1));i<0;i++)
{
cout<<"\n Enter Initial Condition for y["<<i<<"] = ";
cin>>y[i]; // past condn forced to -1 ...
}
    G: cout<<"\n Difference Equation is ==> { ";
for(i=0;i<100;i++)
{
y[i]=0;
for(j=0;j<M;j++)
y[i]=y[i]-(b[j]*y[i-j]);
for(j=0;j<N;j++)
if(i-j>=0 && i-j<N)
y[i]=y[i]+(a[j]*x[i-j]);
cout<<" "<<y[i];


}
cout<<" }";


     A: cout<<"\n Do You Want To Change Input Condition ? [1/0]==> ";
cin>>ein;
if(ein==1)
goto D;
cout<<"\n Do You Want To Change Initial Condition ? [1/0]==> ";
cin>>einit;
if(einit==1)
goto E;
cout<<"\n Do You Want To Change Whole System ? [1/0]==> ";
cin>>es;
if(es==1)
goto F;
     getch();
}
/******************** Output *******************/


/* How Many Terms of Y (M+1)? ==> 2
                                                                                
 How Many Terms of X (N+1)? ==> 3                                               
                                                                                
 Enter Coefficients Terms of X ==>  1 0 0                                       
                                                                                
 Enter x(N) ==>  1 2 3                                                          
                                                                                
 Enter Coefficients Terms of Y ==>  1 -1                                        
                                                                                
 Enter Initial Condition for y[-1] = 1                                          
                                                                                
 Difference Equation is ==> {  2 4 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 }        
 Do You Want To Change Input Condition ? [1/0]==> 0                             
                                                                                
 Do You Want To Change Initial Condition ? [1/0]==> 0                           


 Do You Want To Change Whole System ? [1/0]==> 0 
*/

Linear Convolution Problem...


Prog :- Find Output of a given System for given input sequence using Linear Convolution.
/**************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<math.h>
/**************************************************************************/
class list
{
    public:
int index,value,length;
list *next;
list()
{
value=0;
}
list *accepthn(list *w);
list *acceptxn(list *w);
list *convol(list *w,list *v);
};
list *p,*q,*yy,*qq,*r,*head,*head1,*head2;
list *f,*h,*x,*head3;
int i,j,k,l,m,e,xe,he;
int y[201]={0};
int xlen,hlen,max,size,xlo,xup,hlo,hup;
/**************************************************************************/
list *list::accepthn(list *w)
{
   i=0;
   head=q=w;
   do
   {
       p=new list;
       cout<<"\n Enter index & Element ==> ";
       cin>>p->index>>p->value;
       p->next=NULL;
       if(head==NULL)
       {
 head=p;
 q=head;
       }
       else
       {
 q->next=p;
 q=p;
       }
       cout<<"\n Continue ? [1/0]==> ";
       cin>>e;
       i++;
       q->length++;
   }while(e==1);
   return(head);
}
/**************************************************************************/
list *list::convol(list *x,list *h)
{
   cout<<"\n \n Impulse Response Of Given System is ==>  \n{ ";
   for(i=(xlo+hlo);i<=(xup+hup);i++)
  y[i]=0;
   for(i=(xlo+hlo);i<=(xup+hup);i++)
   {
y[i]=0;
head1=x;
head2=h;
for(head2=h;head2!=NULL;head2=head2->next)
{
   for(head1=x;head1!=NULL;head1=head1->next)
   {
if(head1->index==i-head2->index)
    y[i]=y[i]+(head1->value*head2->value);
   }
}
cout<<y[i]<<" ";
   }
   cout<<" }";
}
/**************************************************************************/
void main()
{
   clrscr();
B: xlo=hlo=100; //Accepting System Input h(n)
   xup=hup=-100;
   cout<<"\n System h(n) ==>  ";
   h=r->accepthn(NULL);
   cout<<"\n h(n) ==> { ";
   head1=h;
   do
   {
  if(head1->index==0)
    cout<<" @";
cout<<" -> "<<head1->value;
if(head1->index>hup)
  hup=head1->index;
if(head1->index<hlo)
  hlo=head1->index;
head1=head1->next;
   }while(head1!=NULL);
   cout<<" }";
A: xlo=100;        //Accepting Input x(n)
   xup=-100;
   cout<<"\n Input x(n) ==>  ";
   x=r->accepthn(NULL);
   cout<<"\n h(n) ==> { ";
   head1=x;
   do
   {
    if(head1->index==0)
  cout<<" @";
cout<<" -> "<<head1->value;
if(xup<head1->index)
  xup=head1->index;
if(xlo>head1->index)
  xlo=head1->index;
head1=head1->next;
}while(head1!=NULL);
yy=r->convol(x,h);
cout<<"\n Do You Want to change input x(n) [1/0] => ";
cin>>xe;
if(xe==1)
  goto A;
cout<<"\n Do You Want to change System Imput h(n) [1/0] => ";
cin>>he;
if(he==1)
  goto B;
getch();
}
/********************************** Output **********************************/
 System h(n) ==>                                                                
 Enter index & Element ==> 0 1                                                  
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> -1 1                                                 
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> 1 1                                                  
                                                                                
 Continue ? [1/0]==> 0                                                          
                                                                                
 h(n) ==> {  @ -> 1 -> 1 -> 1 }                                                 
 Input x(n) ==>                                                                 
 Enter index & Element ==> -99 2                                                
                                                                                
 Continue ? [1/0]==> 1                                                          
                                                                                
 Enter index & Element ==> 99 3                                                 
                                                                                
 Continue ? [1/0]==> 0                                                          
                                                                                
 h(n) ==> {  -> 2 -> 3                                                          
                                                                                
 Impulse Response Of Given System is ==>                                        
{ 2 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 3 3 3  }          
 Do You Want to change input x(n) [1/0] => 0                                    
                                                                                
 Do You Want to change System Imput h(n) [1/0] => 0                             
*/