Bài 1: Nhập tín hiệu vào x(n) và đáp ứng xung là h(m) ==> Tính Y(n)
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
void nhap(int mang[],int n)
{
int i,k;
for(i=0;i<=n;i++)
{
cout<<"\nNhap phan thu :"<<i+1<<":";
cin>>k;
mang[i]=k;
}
}
void xem(int a[],int t)
{
int i;
for(i=0;i<=t;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
}
void khoitao(int y[],int t)
{
for(int i=0;i<=t;i++)
{
y[i]=0;
}
}
void tinh_y(int x[],int h[],int y[],int n,int m)
{
khoitao(y,m+n);
for(int i=0;i<=n;i++)
for(int j=i;j<=m+i;j++)
y[j]+=x[i]*h[j-i];
}
void min_max(int x[],int n,int *min,int *max)
{
for(int i=1;i<=n;i++)
{
if(*max<=x[i]) *max=x[i];
if(*min>=x[i])*min=x[i];
}
}
void ve_hinh(int x[],int n,int min,int max)
{
float tyle_ox=0.0,tyle_oy=0.0,k=100;
tyle_oy=450/(abs(min)+max);
tyle_ox=600/(n+4);
//Ve chu chay va xem ty le cua ox,oy
for(int i1=0;i1<=100;i1++)
{ cleardevice();
outtextxy(getmaxx()/2-i1,i1+i1+i1,"SAU DAY TY LE CUA MAN HINH VA TIN HIEU ");
outtextxy(20,i1+i1+i1,"VUI LONG DOI MOT CHUT =>");
delay(10);
}
cout<<"\nDo co gian cua ox="<<tyle_ox<<" so voi man hinh";
cout<<"\nDo co gian cua oy="<<tyle_oy<<" so voi man hinh";
getch();
cleardevice();
//ve truc toa do
float td_y=tyle_oy*max+20,td_x1=getmaxx()-5,td_x2=getmaxx()-10;
char *inraso;
setcolor(15);
line(5,td_y,td_x1,td_y);
line(td_x1,td_y,td_x2,td_y-5);
line(td_x1,td_y,td_x2,td_y+5);
outtextxy(td_x2,td_y-15,"n");
for(int i=0;i<=n;i++)
{ setcolor(14);
line(k,td_y,k,td_y-tyle_oy*x[i]);
pieslice(k,td_y-tyle_oy*x[i],0,360,2);
sprintf(inraso,"%d",i);
outtextxy(k-10,td_y+5,inraso);
sprintf(inraso,"%d",x[i]);
if(x[i]>=0)
outtextxy(k,td_y-tyle_oy*x[i]-10,inraso);
else
outtextxy(k+3,td_y-tyle_oy*x[i],inraso);
k=k+tyle_ox;
}
}
void ve_3h(int x[],int n,int min,int max,int y)
{
float ox=0,oy=0;
ox=600/(n+4);
oy=160/(abs(min)+max);
//Ve Hinh cua X(n)---------------------------------------------------------
//Thong tin Sinh Vien-------------------
setcolor(14);
outtextxy(200,5,"PHAN VAN NGON MSV:0751070341 -48K-Tin-DHV");
if(y<120)
{
outtextxy(5,y-20,"TIN HIEU VAO");
outtextxy(10,y-10,"x(n)");
}
else
if(y<280)
{
outtextxy(5,y-20,"DAP UNG XUNG");
outtextxy(10,y-10,"h(n)");
}
else
{
outtextxy(5,y-20,"TIN HIEU RA");
outtextxy(10,y-10,"y(n)");
}
int k=120;
int x1=getmaxx()-5,x2=getmaxx()-10;
getch();
//Ve truc toa do cho x(n)
setcolor(15);
line(0,y,x1,y);
line(x2,y-5,x1,y);
line(x2,y+5,x1,y);
outtextxy(x2-2,y-10,"n");
//Ve cac tin hieu cua x(n)
char *inso;
for(int i=0;i<=n;i++)
{ setcolor(12);
line(k,y,k,y-x[i]*oy);
pieslice(k,y-x[i]*oy,0,360,2);
sprintf(inso,"%d",i);
setcolor(14);
outtextxy(k-10,y+5,inso);
sprintf(inso,"%d",x[i]);
if(x[i]<=0)
outtextxy(k,y+5-x[i]*oy,inso);
else
outtextxy(k,y-10-x[i]*oy,inso);
k=k+ox;
delay(10);
}
}
void phanvanngon()
{
outtextxy(10,getmaxy()-30,"PHAN VAN NGON ");
outtextxy(10,getmaxy()-20,"MSV:0751070341");
outtextxy(10,getmaxy()-10,"LOP:48K DHV");
}
void main()
{
int i,j,x[100],h[100],y[100],n,m,ox,oy;
int gd=0,gm;
initgraph(&gd,&gm,"D:\\TC\\BGI");
if(graphresult()!=grOk) exit(1);
cout<<"\nNhap n cua tin hieu vao :";cin>>n;
cout<<"\nNhap n cua dap ung xung :";cin>>m;
//Nhap tin hieu vao---------------------------------------
nhap(x,n);
cout<<"\nXem Tin hieu vao :";
xem(x,n);
//Nhap dap ung xung---------------------------------------
nhap(h,m);
cout<<"\nXem dap ung xung:";
xem(h,m);
//Tinh tin hieu ra----------------------------------------
khoitao(y,m+n);
tinh_y(x,h,y,n,m);
cout<<"\nXem Tin hieu ra:";
xem(y,m+n);
//Man hinh cho--------------------------------------------
cout<<"\n\nBan bam ENTER de xem cac tin hieu:\n";
getch();
cleardevice();
//Ve tin hieu vao x(n)-----------------------------------
int max=x[0],min=x[0];
min_max(x,n,&min,&max);
ve_hinh(x,n,min,max);
outtextxy(10,5,"TIN HIEU VAO");
outtextxy(20,20,"x(n)");
phanvanngon();
getch();
cleardevice();
//Ve dap ung xung h(n)------------------------------------
int max1=h[0],min1=h[0];
min_max(h,m,&min1,&max1);
ve_hinh(h,m,min1,max1);
outtextxy(10,5,"DAP UNG XUNG");
outtextxy(20,20,"h(n)");
phanvanngon();
getch();
cleardevice();
//Ve tin hieu ra y(n)-------------------------------------
int max2=y[0],min2=y[0];
min_max(y,m+n,&min2,&max2);
ve_hinh(y,m+n,min2,max2);
outtextxy(10,5,"TIN HIEU RA");
outtextxy(20,20,"y(n)");
phanvanngon();
getch();
cleardevice();
//Ve xem ca 3 tin hieu tren 1 man hinh
ve_3h(x,n,min2,max2,70);
ve_3h(h,m,min2,max2,200);
ve_3h(y,n+m,min2,max2,370);
getch();
closegraph();
}
Cách 2:
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
void nhap(int x[],int n)
{
int i,k;
for(i=0;i<=n;i++)
{
cout<<"\nNhap phan thu :"<<i+1<<":";
cin>>k;
x[i]=k;
}
}
void xem(int a[],int t)
{
int i;
for(i=0;i<=t;i++)
{
cout<<a[i]<<" ";
}
cout<<"\n";
}
void khoitao(int y[],int t)
{
for(int i=0;i<=t;i++)
{
y[i]=0;
}
}
void tinh_y(int x[],int h[],int y[],int n,int m)
{
khoitao(y,m+n);
for(int i=0;i<=n;i++)
for(int j=i;j<=m+i;j++)
y[j]+=x[i]*h[j-i];
}
void min_max(int x[],int n,int *min,int *max)
{
for(int i=1;i<=n;i++)
{
if(*max<=x[i]) *max=x[i];
if(*min>=x[i])*min=x[i];
}
}
void ve_hinh(int x[],int n,int min,int max)
{
float tyle_ox=0.0,tyle_oy=0.0,k=100;
tyle_oy=450/(abs(min)+max);
tyle_ox=600/(n+2);
cout<<"\ntyle_ox="<<tyle_ox<<" so voi man hinh";
cout<<"\nTyle_oy="<<tyle_oy<<" so voi man hinh";
getch();
cleardevice();
//ve truc toa do
float toado=tyle_oy*max+20;
setcolor(15);
line(10,toado,getmaxx()-5,toado);
for(int i=0;i<=n;i++)
{ setcolor(14);
line(k,toado,k,toado-tyle_oy*x[i]);
pieslice(k,toado-tyle_oy*x[i],0,360,2);
k=k+tyle_ox;
}
}
void main()
{
int i,j,x[100],h[100],y[100],n,m,ox,oy;
int gd=0,gm;
initgraph(&gd,&gm,"D:\\TC\\BGI");
if(graphresult()!=grOk) exit(1);
cout<<"\nNhap n=";cin>>n;
cout<<"\nNhap m=";cin>>m;
nhap(x,n);
int max=x[0],min=x[0];
min_max(x,n,&min,&max);
getch();
cleardevice();
ve_hinh(x,n,min,max);
getch();
cleardevice();
nhap(h,m);
int max1=h[0],min1=h[0];
min_max(h,m,&min1,&max1);
getch();
cleardevice();
ve_hinh(h,m,min1,max1);
khoitao(y,m+n);
cout<<"\nTin hieu ra:\n";
tinh_y(x,h,y,n,m);
getch();
cleardevice();
int max2=y[0],min2=y[0];
min_max(y,m+n,&min2,&max2);
ve_hinh(y,m+n,min2,max2);
getch();
closegraph();
}
cách 3:
#include<conio.h>
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<dos.h>
#include<graphics.h>
#include<math.h>
void main()
{
int gd=0,gm,k,ox,oy,n,m;
long int h[100],x[100],y[100],a[100];
initgraph(&gd,&gm,"D:\\TC\\BGI");
if(graphresult()!=grOk) exit(1);
randomize();
//Khoi tao n,m-----------------------------------
cout<<"\nNhap n cua X(n)=";cin>>n;
cout<<"\nNhap m cua h(m)=";cin>>m;
//Nhap x(n)------------------------
cout<<"\n-----Moi ban nhap x(n)-----------------------------\n";
for(int i1=0;i1<=n;i1++)
{
cout<<"x("<<i1<<")=";
cin>>x[i1];
}
//In x(n)----------------------------------
cout<<"\nXem Tin hieu x(n)";
for(int i4=0;i4<=n;i4++)
{
cout<<x[i4]<<" ";
}
//Nhap h(m)--------------------------
cout<<"\n------Moi ban nhap h(m)----------------------------\n";
for(int i2=0;i2<=m;i2++)
{
cout<<" h("<<i2<<")=";
cin>>h[i2];
if(i2==m)
{
for(int j1=1;j1<=n;j1++)
{
h[i2+j1]=0;
}
}
}
//In h(m+n) ----------------------------
cout<<"\nXem tin hieu h(n)";
for(int i5=0;i5<=m+n;i5++)
{
cout<<" "<<h[i5];
}
//Khoi tao gia tri ban dau cho y(n)------------------
for(int i7=0;i7<=m+n;i7++)
{
y[i7]=0;
}
//Tinh Y(n)=--------------------
for(int i3=0;i3<=n;i3++)
{
for(int j3=0,j4=0;j3<=m,j4<n+m;j3++)
{
a[j3]=h[j3]*x[i3];
j4=i3+j3;
y[j4]=y[j4]+a[j3];
}
}
//In h1(m+n)-----------------------------------------
cout<<"\nXem Ket qua y(n)=x(n)*h(n)=";
for(int i6=0;i6<=n+m;i6++)
{
cout<<", "<<y[i6];
}
//Tim max cua y de gian man hinh cho hop voi tin hieu
int max=abs(y[0]);
for(int i14=0;i14<=n+m;i14++)
{
if(max<=abs(y[i14]))
max=abs(y[i14]);
}
//Lay toa do y hop voi tin hieu
float l=100.0/max;
cout<<"\nMax="<<max<<"\nl="<<l;
cout<<"\nBan an ENTER de xem tin hieu vao va ra";
getch();
//Ve Hinh cua X(n)---------------------------------------------------------
//Xoa Man hinh anh cu tren man hinh
cleardevice();
//Thong tin Sinh Vien-------------------
setcolor(14);
outtextxy(200,5,"PHAN VAN NGON MSV:0751070341 -48K-Tin-DHV");
outtextxy(5,50,"TIN HIEU VAO");
outtextxy(10,60,"x(n)");
k=120;
getch();
//Ve truc toa do cho x(n)
setcolor(15);
line(0,70,getmaxx()-5,70);
line(getmaxx()-10,65,getmaxx()-5,70);
line(getmaxx()-10,75,getmaxx()-5,70);
outtextxy(getmaxx()-12,60,"n");
//Ve cac tin hieu cua x(n)
ox=600/(m+n+4);
char *inso1;
for(int i8=0;i8<=n;i8++)
{ setcolor(12);
line(k,70,k,70-x[i8]*l);
pieslice(k,70-x[i8]*l,0,360,2);
sprintf(inso1,"%d",i8);
setcolor(14);
outtextxy(k-10,75,inso1);
k=k+ox;
delay(1000);
}
//Ve Hinh h(m)----------
//Xuat chu DAP UNG XUNG
outtextxy(5,180,"DAP UNG XUNG");
outtextxy(10,190,"h(n)");
getch();
//Ve truc toa do cho h(m)
k=120;
setcolor(15);
line(0,200,getmaxx()-5,200);
line(getmaxx()-10,195,getmaxx()-5,200);
line(getmaxx()-10,205,getmaxx()-5,200);
outtextxy(getmaxx()-12,190,"n");
//Ve cac tin hieu cua h(m)
setcolor(14);
ox=600/(m+n+4);
for(int i9=0;i9<=m;i9++)
{ setcolor(13);
line(k,200,k,200-h[i9]*l);
pieslice(k,200-h[i9]*l,0,360,2);
sprintf(inso1,"%d",i9);
setcolor(14);
outtextxy(k-10,205,inso1);
k=k+ox;
delay(1000);
}
//Ve Tin Hieu ra y(n)
//Xuat chu Tin Hieu ra
outtextxy(5,350,"TIN HIEU RA");
outtextxy(10,360,"y(n)");
getch();
//Ve truc toa do cho y(n)
k=120;
setcolor(15);
line(0,370,getmaxx()-5,370);
line(getmaxx()-10,365,getmaxx()-5,370);
line(getmaxx()-10,375,getmaxx()-5,370);
outtextxy(getmaxx()-12,360,"n");
//Ve cac tin hieu y(n)
setcolor(15);
ox=600/(m+n+4);
for(int i13=0;i13<=n+m;i13++)
{ setcolor(3);
line(k,370,k,370-y[i13]*l);
pieslice(k,370-y[i13]*l,0,360,2);
sprintf(inso1,"%d",i13);
setcolor(14);
outtextxy(k-10,375,inso1);
k=k+ox;
delay(10);
}
getch();
closegraph();
}