Advertisement
tei123

wajs

Apr 24th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.84 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <conio.h>
  4.  
  5. double X(double t) {
  6. return(1.0*sin(2.0*M_PI*10000.0*t)*cos(2.0*M_PI*20000.0*t));
  7. }
  8.  
  9. struct complexx {
  10. double Re;
  11. double Im;
  12. };
  13. struct complexx cmplx(double A, double B) {
  14. struct complexx result;
  15. result.Re=A;
  16. result.Im=B;
  17. return(result);
  18. }
  19. struct complexx cMult(struct complexx A, struct complexx B) {
  20. struct complexx result;
  21. result.Re=A.Re*B.Re-A.Im*B.Im;
  22. result.Im=A.Re*B.Im+A.Im*B.Re;
  23. return(result);
  24. }
  25. struct complexx cDiv(struct complexx nr, struct complexx d) {
  26. double btm, re_top, im_top;
  27. struct complexx result;
  28. btm=d.Re*d.Re+d.Im*d.Im;
  29. re_top=nr.Re*d.Re+nr.Im*d.Im;
  30. im_top=nr.Im*d.Re-nr.Re*d.Im;
  31. result.Re=re_top/btm;
  32. result.Im=im_top/btm;
  33. return(result);
  34. }
  35. struct complexx cAdd(struct complexx A, struct complexx B) {
  36. struct complexx result;
  37. result.Re=A.Re+B.Re;
  38. result.Im=A.Im+B.Im;
  39. return(result);
  40. }
  41. int main(void) {
  42. //clrscr();
  43. double x[8];
  44. struct complexx z[8];
  45. int N=8;
  46. int n,m;
  47. struct complexx sXz=cmplx((double)0.0, (double)0.0);
  48. double re,im;
  49. double h,fs;
  50. fs=80000;
  51. h=1.0/fs;
  52. double sX;
  53. double sY;
  54. double XX[8],YY[8];
  55. for(m=0;m<N;m=m+1) {
  56. sX=0.0;
  57. sY=0.0;
  58. for(n=0;n<N;n=n+1) {
  59. sX=sX+X(n*h)*cos(2.0*M_PI*(double)n*(double)m/(double)N);
  60. sY=sY-X(n*h)*sin(2.0*M_PI*(double)n*(double)m/(double)N);
  61. }
  62. XX[m]=sX;
  63. YY[m]=sY;
  64. printf("\n DFT X(%d)=%12.4f",m,*(XX+m));
  65. printf(" Y(%d)=%12.4f",m,*(YY+m));
  66. }
  67. for(n=0;n<N;n=n+1) {
  68. sXz=cmplx(0.0, 0.0);
  69. for(m=0;m<N;m=m+1) {
  70. sXz=cAdd(sXz,cMult(cmplx(XX[m],YY[m]),cmplx(cos(2.0*M_PI*n*m/N),sin(2.0*M_PI*n*m/N))));
  71.  
  72. }
  73. printf("\n IDFT");
  74. printf(" x(%d) %12.4f ",n,cDiv(sXz,cmplx(N,0.0)));
  75. }
  76. printf("\n probki sygnalu");
  77. for(n=0;n<N;n=n+1) printf("\n x[%d]=%12.4f ",n,X(n/fs));
  78. int abc;
  79. scanf("%d", &abc);
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement