Studentenversion des ESY6/A Praktikums "signal_processing".
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Complex.c 1.8KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "Complex.h"
  2. #include <math.h>
  3. float complex_abs( const Complex * c ) {
  4. return sqrt( pow( c->re, 2 ) + pow( c->im, 2 ) );
  5. }
  6. float complex_arg( const Complex * a )
  7. {
  8. return atan2( a->im, a->re );
  9. }
  10. Complex complex_comjunction( const Complex * a ) {
  11. Complex c = { a->re, - a->im };
  12. return c;
  13. }
  14. float complex_phi( const Complex * c ) {
  15. float phi = 0.0;
  16. // quadrant I
  17. if ( c->re >= 0 && c->im >= 0 ) phi = atan( c->im / c->re );
  18. // quadrant II, III
  19. if ( c->re < 0 ) phi = atan( c->im / c->re ) + M_PI;
  20. // quadrant IV
  21. if ( c->re > 0 && c->im < 0 ) phi = atan( c->im / c->re ) + 2 * M_PI;
  22. return phi * 180.0 / M_PI;
  23. }
  24. Complex complex_scalar_mul( const Complex * a, const float b ) {
  25. Complex c = { 0.0, 0.0 };
  26. float o_re = b;
  27. float o_im = 0.0;
  28. c.re = a->re * o_re - a->im * o_im;
  29. c.im = a->re * o_im + o_re * a->im;
  30. return c;
  31. }
  32. Complex complex_mul( const Complex * a, const Complex * b ) {
  33. Complex c = { 0.0, 0.0 };
  34. float o_re = b->re;
  35. float o_im = b->im;
  36. c.re = a->re * o_re - a->im * o_im;
  37. c.im = a->re * o_im + o_re * a->im;
  38. return c;
  39. }
  40. Complex complex_add( const Complex * a, const Complex * b ) {
  41. Complex c = { 0.0, 0.0 };
  42. c.re = a->re + b->re;
  43. c.im = a->im + b->im;
  44. return c;
  45. }
  46. Complex complex_sub( const Complex * a, const Complex * b ) {
  47. Complex c = { 0.0, 0.0 };
  48. c.re = a->re - b->re;
  49. c.im = a->im - b->im;
  50. return c;
  51. }
  52. Complex complex_polar( float roh, float theta )
  53. {
  54. Complex c = { roh * cos( theta ), roh * sin( theta ) };
  55. return c;
  56. }
  57. Complex complex_pow( const Complex * a, int n )
  58. {
  59. float roh = pow( complex_abs( a ), n );
  60. float theta = complex_arg( a );
  61. return complex_polar( roh, ( float )( n ) * theta );
  62. }