/******************************************************************************
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
*/