martaczaska

sinus_2_completed

May 2nd, 2020
536
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.04 KB | None | 0 0
  1. #include <dsplib.h>
  2.  
  3. #define NUM_SAMPLES 5000
  4. #define krok 137
  5.  
  6. int pila[NUM_SAMPLES];
  7. int prostokatny[NUM_SAMPLES];
  8. int trojkatny[NUM_SAMPLES];
  9. int sinus[NUM_SAMPLES];
  10. int dsplib_sin[NUM_SAMPLES];
  11. int white_noise[NUM_SAMPLES];
  12.  
  13. void saw(int* tablica, unsigned int dl_tablicy, unsigned int step){
  14.     int amplituda = 0;
  15.     int i;
  16.     tablica[0] = 0;
  17.     for(i = 1; i < dl_tablicy; i++){
  18.         amplituda = amplituda + step;
  19.         tablica[i] = amplituda;
  20.     }
  21. }
  22.  
  23. void rect(int* tablica_prost, unsigned int dl_tablicy, unsigned int step, int prog){
  24.         int amplituda = 0;
  25.         int i;
  26.         for(i = 1; i < dl_tablicy; i++){
  27.             if(amplituda < prog){
  28.                 tablica_prost[i] = 32767;
  29.             }
  30.             else{
  31.                 tablica_prost[i] = -32768;
  32.             }
  33.             amplituda = amplituda + step;
  34.         }
  35. }
  36.  
  37. void tri(int* tablica_tri, unsigned int dl_tablicy, unsigned int step){
  38.     signed int amplituda = 0;
  39.     signed int bezwzgledna = 0;
  40.         int i;
  41.         tablica_tri[0] = 0;
  42.         for(i = 1; i < dl_tablicy; i++){
  43.             amplituda = amplituda + step;
  44.  
  45.             bezwzgledna = amplituda < 0 ? -amplituda : amplituda;
  46.             tablica_tri[i] = ((bezwzgledna<<1) - 32767);
  47.         }
  48. }
  49.  
  50. void sint(int* tablica_sin, unsigned int dl_tablicy, unsigned int step){
  51.         signed int amplituda = 0;
  52.         signed int x = 0;
  53.         int i;
  54.         int minus = 0;
  55.         const long a_1 = 12868; //3.14159 w dziesiętnym (pi)
  56.         const long a_2 = 21167; //5.16771  w dziesiętnym (pi^3/3!)
  57.         const long a_3 = 10445; //2.55016 w dziesiętnym (pi^5/5!)
  58.         const long a_4 = 2455; //0.59926 w dziesiętnym (pi^7/7!)
  59.  
  60.         int dwa = 0;
  61.         int trzy = 0;
  62.         int piec = 0;
  63.         int siedem = 0;
  64.  
  65.         long y = 0;
  66.  
  67.         tablica_sin[0] = 0;
  68.         for(i = 1; i < dl_tablicy; i++){
  69.             amplituda = amplituda + step;
  70.  
  71.             if(amplituda < 0){minus = 1;}
  72.             else{minus = 0;}
  73.  
  74.             x = amplituda < 0 ? -amplituda : amplituda;
  75.             if(x > 16384){
  76.                 x = 32767 - x;
  77.             }
  78.             dwa = _smpy(x, x);
  79.             trzy = _smpy(dwa, x);
  80.             piec = _smpy(dwa, trzy);
  81.             siedem = _smpy(dwa, piec);
  82.  
  83.             y = a_1*x - a_2*trzy + a_3*piec - a_4*siedem;
  84.             //tablica_sin[i] = (int)((y + (1<<11)) >> 12);
  85.             if(minus == 1){
  86.                 //tablica_sin[i] = -tablica_sin[i];
  87.                 tablica_sin[i] = -(int)((y + (1<<11)) >> 12);
  88.             }
  89.             else{
  90.                 tablica_sin[i] = (int)((y + (1<<11)) >> 12);
  91.             }
  92.         }
  93. }
  94.  
  95.  
  96. void main(void) {
  97.     //saw(pila, NUM_SAMPLES, krok);
  98.     //rect(prostokatny, NUM_SAMPLES, krok, 0);
  99.     //tri(trojkatny, NUM_SAMPLES, krok);
  100.     //sint(sinus, NUM_SAMPLES, krok);
  101.     //ushort oflag = sine (DATA *pila, DATA *dsplib_sin, 5000);
  102.     sine((DATA*)pila, (DATA*)dsplib_sin, NUM_SAMPLES);
  103.     //rand16init();
  104.     //rand16((DATA*)white_noise, NUM_SAMPLES);
  105.  
  106.     while (1); // do not exit
  107. }
Advertisement
Add Comment
Please, Sign In to add comment