Advertisement
stiansjogren

SigTech

Jun 2nd, 2016
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.56 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "complex.h"
  5.  
  6. #define FS 32.78 *pow(10,6)
  7.  
  8.  
  9. void gen_sig(float *sig, int size);
  10.  
  11. void g_sig(struct complex *data, struct complex32 *data32, struct complex16 *data16,int size);
  12.  
  13. int main(){
  14.     const int size = 307200;
  15.     printf("%d\n", size);
  16.     float sig[size];
  17.     struct complex *data;
  18.     struct complex32 *data32;
  19.     struct complex16 *data16;
  20.  
  21.  
  22.  
  23.  
  24.  
  25.     //data   = malloc(sizeof(struct complex));
  26.    // data32 = malloc(sizeof(struct complex32));
  27.     //data16 = malloc(sizeof(struct complex16));
  28.  
  29.     printf("FS: %f\n" ,FS);
  30.     if ((data=malloc(sizeof(struct complex)*(size_t)size))==NULL)
  31.     {
  32.       fprintf(stderr, "Out of memory!\n");
  33.       exit(1);
  34.     }
  35.  
  36.   if ((data16=malloc(sizeof(struct complex16)*(size_t)size))==NULL)
  37.     {
  38.       fprintf(stderr, "Out of memory!\n");
  39.       exit(1);
  40.     }
  41.  
  42.   if ((data32=malloc(sizeof(struct complex32)*(size_t)size))==NULL)
  43.     {
  44.       fprintf(stderr, "Out of memory!\n");
  45.       exit(1);
  46.     }
  47.  
  48.  
  49.     g_sig(data,data32, data16, size);
  50.    
  51. return 0;
  52. }
  53.  
  54. void g_sig(struct complex *data, struct complex32 *data32, struct complex16 *data16,int size){
  55.  
  56.     int write = 0;
  57.     int j;
  58.     // Generating a table got 100 MHz channel
  59.     int cp0 = 160; // First cyclic prefix
  60.     int cp  = 144; // Normal cyclic prefix
  61.     int cpe = 512; // Extended cyclic prefix
  62.     int deltaF = 200; //Hz
  63.     int ofdm_size = 2048;
  64.     for (j=0;j<size;j++){
  65.         data[j].r = cos(20*pow(10,6)*j/FS);//Is is Mhz on this line;
  66.         data[j].i = sin(20*pow(10,6)*j/FS);
  67.     }
  68.     //printf("%d",(1<<15));
  69.     //Truncate data to fixed-point representation
  70.     // I've used 16 and 32 bits representation,
  71.     for (j=0; j<size;j++){
  72.         data16[j].r = (short)data[j].r*(1<<15)-1;
  73.         data16[j].i = (short)data[j].i*(1<<15)-1;
  74.         data32[j].r = (int)data[j].r*(1<<15)-1;
  75.         data32[j].i = (int)data[j].i*(1<<15)-1;
  76.         printf("%f\n %f\n", data16[j].r, data16[j].i);
  77.                        
  78.     }
  79. }
  80.  
  81.  
  82.  
  83.  
  84.  
  85. void gen_sig(float *sig, int size){
  86.     int write = 0;
  87.     // Generating a table got 100 MHz channel
  88.     int cp0 = 160; // First cyclic prefix
  89.     int cp  = 144; // Normal cyclic prefix
  90.     int cpe = 512; // Extended cyclic prefix
  91.     int deltaF = 200; //Hz
  92.     int ofdm_size = 2048;
  93.    
  94.     if (write){ //write to file.txt
  95.         FILE *f;
  96.         f = fopen("file.txt", "w");
  97.         if(f==NULL){
  98.             exit(1);
  99.         }
  100.         fprintf(f, "x[%d]={ ", size);
  101.            
  102.         for (int i=1;i<size; i+=2){
  103.             sig[i-1] = sin(20*i/FS);
  104.             sig[i]   = cos(20*i/FS);
  105.             fprintf(f, "%f, ", sig[i-1]);
  106.         }
  107.         fprintf(f,"}; \n");
  108.     }  
  109.     for (int i=1;i<size; i+=2){
  110.         sig[i-1] = sin(20*i/FS);
  111.         sig[i]   = cos(20*i/FS);
  112.     }
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement