I am trying to send out a signal and compare the phase shift to the input. I can get the phase shift but it is different every time. The issue seems to be that the starting point for the data is not consistently at the same value. Is there a way to make sure this starting value stays constant every time the program is run?
I am also wondering if what I am trying to do is possible with the C code or if moving to FPGA programming is necessary.
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
#include "redpitaya/rp.h"
int main(int argc, char **argv){
/* Print error, if rp_Init() function failed */
if(rp_Init() != RP_OK){
fprintf(stderr, "Rp api init failed!\n");
}
//################################SET UP BUFFER#########################################
uint32_t buff_size = 16384; //16384
float *buff = (float *)malloc(buff_size * sizeof(float));
//################################GENERATE MODULATING WAVE#########################################
rp_AcqReset();
rp_GenReset();
rp_GenWaveform(RP_CH_1, RP_WAVEFORM_SINE);
rp_GenOutEnable(RP_CH_1);
rp_AcqSetDecimation(RP_DEC_1);
rp_AcqSetTriggerDelayNs(133340);
//################################SET UP ACQUIRING THE SIGNAL#########################################
rp_GenAmp(RP_CH_1, 1.0);
rp_GenFreq(RP_CH_1, 10000000.0);
rp_AcqStart();
sleep(0.0001333); //16385/125000000
//################################READ THE BUFFER#########################################
rp_GenTrigger(RP_CH_1);
rp_AcqGetOldestDataV(RP_CH_1, &buff_size, buff);
//###############################PHASE SHIFTING##########################################
int i;
double totalReal = 0;
double totalImaginary = 0;
double mixReal[buff_size];
double mixImaginary[buff_size];
double phi = 0;
//16385
for(i = 0; i < buff_size; i++){
mixReal[i] = buff[i]*sin(((double)i/122880000)*2*3.14*10000000);//need to change argument based on sampling rate
mixImaginary[i] = buff[i]*cos(((double)i/122880000)*2*3.14*10000000);
totalReal += mixReal[i];
totalImaginary += mixImaginary[i];
}
phi = atan((totalImaginary/buff_size) / (totalReal/buff_size));
//rp_GenOutDisable(RP_CH_1);
int j;
FILE * fp;
fp = fopen ("testoutput.txt", "w+");
for(j = 0; j < buff_size; j++){
printf("%f\n", buff[j]);
fprintf(fp, "%f\n",buff[j]);
}
printf("%f\n", phi);
fclose(fp);
/* Releasing resources */
free(buff);
rp_Release();
return 0;
}