martaczaska

sinus_1

May 2nd, 2020
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 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;
  56.         const long a_2 = 21167; //5.16771  w dziesiętnym;
  57.         const long a_3 = 10445; //2.55016 w dziesiętnym;
  58.         const long a_4 = 2455; //0.59926 w dziesiętnym;
  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.             if(amplituda < 0){
  71.                 minus = 1;
  72.             }
  73.             x = amplituda < 0 ? -amplituda : amplituda;
  74.             if(x > 0.5){
  75.                 x = 32767 - x;
  76.             }
  77.             dwa = _smpy(x, x);
  78.             trzy = _smpy(dwa, x);
  79.             piec = _smpy(dwa, trzy);
  80.             siedem = _smpy(dwa, piec);
  81.  
  82.             y = a_1*x - a_2*trzy + a_3*piec - a_4*siedem;
  83.             tablica_sin[i] = (int)((y + (1<<11)) >> 12);
  84.             if(minus == 1){
  85.                 tablica_sin[i] = -tablica_sin[i];
  86.             }
  87.         }
  88. }
  89.  
  90.  
  91. void main(void) {
  92.     //saw(pila, NUM_SAMPLES, krok);
  93.     //rect(prostokatny, NUM_SAMPLES, krok, 0);
  94.     //tri(trojkatny, NUM_SAMPLES, krok);
  95.     sint(sinus, NUM_SAMPLES, krok);
  96.     //ushort oflag = sine (DATA *pila, DATA *dsplib_sin, 5000);
  97.     //sine((DATA*)pila, (DATA*)dsplib_sin, NUM_SAMPLES);
  98.     //rand16init();
  99.     //rand16((DATA*)white_noise, NUM_SAMPLES);
  100.  
  101.     while (1); // do not exit
  102. }
Advertisement
Add Comment
Please, Sign In to add comment