-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignalAnalyzerShared.c
More file actions
1463 lines (1177 loc) · 52.1 KB
/
SignalAnalyzerShared.c
File metadata and controls
1463 lines (1177 loc) · 52.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "toolbox.h"
#include "SignalAnalyzerShared.h"
#include "CurveFitting.c"
int getTrace(int measurementType, int useCalibration, int trialsToRun, double avgStartFreq, BufferControl *bufferControl, int *panel, int status, int magChart, int phaseChart, int signalsPlot, int freqLabel, char inputChan[CHAN_NAME_LEN], char outputChan[CHAN_NAME_LEN], char refChan[CHAN_NAME_LEN], char trigChan[CHAN_NAME_LEN], uInt8 useRefChan, int useTrigChan, int inputConfigType, double startParam, double stopParam, int stepType, unsigned short stepsPerUnit, double freqOut, double inputVoltage, double outputVoltage, int inputVoltageFollowsOutput, int fitFromEnd, double delayTime, double settleTime, int *vnaRunning, int *requestStop, double **vnaFreqData, double **vnaOutputData, double **vnaMagData, double **vnaPhaseData, int *vnaArraySize, int *vnaDataPoints, int useExternalAmp, double ampGain, int useDivider, double dividerRatio)
{
//To do:
// 0. Incorporate gain and divider into measurements and panel.
// 1. Figure out buffer sizes. Do we want to use the max FIFO buffer size or is a larger one like the one we're doing now okay? Do we want only to generate one cycle and repeat that for our measurements? Yes we do.
// 2. Test with different devices.
// 3. Incorporate cal code. Test code with HPF and LPF. Will the cal work equily well with both circuits?
// 4. Incorporate trigger into SR mode.
// 5. Incorporate every Nth sample drawing and even processing function
// 6. Figure out why phase off @ lower frequencies w/ HPF (probably low signal/noise ratio) and how to fix it.
// Maybe incorporate auto voltage adjustment, or averaging, or both?
// 7. When two channels are read simulatenously, the channels take turns collecting samples. Thus there will be a phase shift between the two waves. Adjust accordinly for more accurate results.
// 8. Incorporate external trigger, just digital.
// 9. Incorporate every Nth sample update function to plot data.
// 10. Figure out why phase off @ lower frequencies w/ HPF (probably low signal/noise ratio) and how to fix it.
TaskHandle inputTaskHandle = 0;
TaskHandle outputTaskHandle = 0;
TaskHandle trigTaskHandle = 0;
int triggerType;
Calibration cal;
int useCal = 0;
//Initializations are here simply to keep the compiler happy and not produce a bunch of warnings.
Device inputDevice = {"", 0, -1, "", -1, -1, -1, -1, -1, -1, -1};
Device outDevice = {"", 0, -1, "", -1, -1, -1, -1, -1, -1, -1};
Device refDevice = {"", 0, -1, "", -1, -1, -1, -1, -1, -1, -1};
Device trigDevice = {"", 0, -1, "", -1, -1, -1, -1, -1, -1, -1};
char freqStr[CHAN_NAME_LEN] = "";
char measurement[CHAN_NAME_LEN] = "";
char unit[CHAN_NAME_LEN] = "";
int32 error = 0;
int totalSteps;
int numTrials = trialsToRun;
double maxRateOut;
double maxRateIn;
//int outputType;
int inputType;
int plotAttr;
int samplesToFit;
unsigned int outputBufferSize;
unsigned int readInBufferSize;
unsigned int dataBufferSize;
int outputBufferMultiplier = 1;
double avgMultiplier = 1.0 / numTrials;
double samplesPerCycle;
double adjustedSamplesPerCycle;
double samplesPerCycleIn;
double adjustedSamplesPerCycleIn;
double computedSamplesPerCycle;
double computedSamplesPerCycleIn;
unsigned int samplesToWrite;
unsigned int samplesToRead;
double rateIn;
double rateOut;
unsigned int cyclesOut = getCyclesOut(freqOut);
int sampleShift = 1;
double mainDataPhase;
double refDataPhase;
int numRead;
int trialCount;
int updateSRBuffer;
float64 *data = NULL;
float64 *averageArray = NULL;
float64 *dataToFit = NULL;
float64 *refDataToFit = NULL;
float64 *wave = NULL;
float64 *currentOutputBuffer = NULL;
float64 *triggerBuff = NULL;
float64 *fittedFunction = NULL;
float64 *xVals = NULL;
double genPhase = 0;
double genAmp = 0;
int freq;
//First things first let's see if everything we need was passed in.
if(!vnaRunning || !vnaFreqData || !vnaOutputData || !vnaMagData || !vnaPhaseData || !vnaArraySize || !vnaDataPoints)
return -1;
//Next let's get our connected device info.
if ((getDeviceInfo(inputChan, &inputDevice) != 0) || (getDeviceInfo(outputChan, &outDevice) != 0) || (useRefChan && (getDeviceInfo(refChan, &refDevice) != 0)) || (useTrigChan && getDeviceInfo(refChan, &refDevice) != 0))
{
if (inputDevice.rateIn == -1 || (useRefChan && refDevice.rateIn == -1) || outDevice.rateOut == -1)
{
MessagePopup("Error", "Could not obtain device info or selected devices do not support I/O. Aborting.");
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
goto Error;
}
}
//Now let's see if we have a valid calibration for this pairing.
useCal = 0;
if (useCalibration)
{
if (ReadCalFromFile(inputDevice.serial, outDevice.serial, calDir, &cal) == 0)
useCal = 1;
else
MessagePopup("Error", "No calibration found for input/output device pair.\nResults will be left uncorrected.");
}
//Next let's get the total number of steps we'll need for our runthrough.
totalSteps = buildStepArray(startParam, stopParam, stepType, stepsPerUnit, 0);
//Now let's prep our data arrays.
if (prepVNAData(totalSteps, vnaFreqData, vnaOutputData, vnaMagData, vnaPhaseData, vnaArraySize, vnaDataPoints) != 0)
{
MessagePopup("Error", "Could not allocate memory for trace acquisition. Stopping operation.");
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
goto Error;
}
//If we're here we succeeded, let's build our parameter array.
if (measurementType == FREQ_MEASUREMENT)
buildStepArray(startParam, stopParam, stepType, stepsPerUnit, *vnaFreqData);
else if (measurementType == VOLT_MEASUREMENT)
buildStepArray(startParam, stopParam, stepType, stepsPerUnit, *vnaOutputData);
//Next let's get our deivce speeds
maxRateIn = inputDevice.rateIn;
maxRateOut = outDevice.rateOut;
//If we're using a ref chan let's see if we need to adjust our input rate.
//This basically is done when the input and reference channels are on the same device.
//The channels have to share resources if they are and so we divide our rate by 2.
if (useRefChan)
maxRateIn = findInputRate(inputDevice, refDevice);
//We need to do a few things next, and how we do them depends on wether we're in VNA or SR mode.
if (measurementType == FREQ_MEASUREMENT)
{
//Again if we're varying the frequencies let's get our max buffer size by seeing the max buffer size of all frequencies.
if (getMaxBufferSizes(*vnaFreqData, totalSteps, maxRateIn, maxRateOut, &readInBufferSize, &outputBufferSize))
{
MessagePopup("Error", "Could not allocate memory for trace acquisition. Stopping operation.");
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
goto Error;
}
//Let's add an extra sample for the last 0.
outputBufferSize++;
//Next let's prep our labels
strcpy(measurement, "Frequency");
strcpy(unit, "Hz");
}
else
{
//Otherwise we're going to have a fixed buffer size since we're only dealing with a single frequency, let's not worry about trying all the different sizes.
if (getMaxBufferSizes(&freqOut, 1, maxRateIn, maxRateOut, &readInBufferSize, &outputBufferSize))
{
MessagePopup("Error", "Could not allocate memory for trace acquisition. Stopping operation.");
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
goto Error;
}
//Now let's double our output buffer multiplier. This is so we can have two buffers between which we can switch.
outputBufferMultiplier *= 2;
//Again let's set up our labels
strcpy(measurement, "Voltage");
strcpy(unit, "V");
}
//And set our input/output type (finite samples, continuous, etc) variables to finite sample output.
readInBufferSize += 2;
dataBufferSize = readInBufferSize;
//Now let's compute our data buffer size, which depends on whether we're using a ref channel or not.
if (useRefChan)
dataBufferSize *= 2;
//Next let's figure out what kind of trigger type we have.
//This is the final piece we need for computing our buffer sizes.
triggerType = getChanType(trigChan);
if (useTrigChan && (triggerType == ANALOG_CHAN))
outputBufferMultiplier *= 2;
//Now lets allocate all our buffers.
wave = (float64*) malloc(outputBufferMultiplier * outputBufferSize * sizeof(float64));
data = (float64*) malloc(dataBufferSize * sizeof(float64));
averageArray = (float64*) malloc(dataBufferSize * sizeof(float64));
fittedFunction = (float64*) malloc(readInBufferSize * sizeof(float64));
xVals = (float64*) malloc(readInBufferSize * sizeof(float64));
//Let's make sure the memory has been allocated.
if (!wave || !data || !averageArray || !fittedFunction || !xVals)
{
MessagePopup("Error","Could not allocate memory for trace acquisition. Stopping operation.");
if(canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
goto Error;
}
//Now if we're using an analog trigger let's prep our square wave.
//TO DO: figure out how to swap this with a PFO trigger buffer rather than an analog out buffer. This is very necessary for the SR triggering.
/* if (useTrigChan && triggerType == ANALOG_CHAN)
{
genPhase = 0;
SquareWave(outputBufferSize, TRIGGER_LEVEL, 1.0 / outputBufferSize, &genPhase, 50.0, wave + outputBufferSize);
if (measurementType == VOLT_MEASUREMENT)
{
genPhase = 0;
SquareWave(outputBufferSize, TRIGGER_LEVEL, 1.0 / outputBufferSize, &genPhase, 50.0, wave + 3 * outputBufferSize);
}
}*/
//Next let's perform the initial configuration of our I/O channels and a few more variables and I'm ready to go.
if (measurementType == FREQ_MEASUREMENT)
{
currentOutputBuffer = wave;
DAQmxErrChk (initVNA(inputChan, outputChan, refChan, trigChan, useRefChan, useTrigChan, freqOut, maxRateOut, maxRateIn, inputVoltage, outputVoltage, dataBufferSize, outputBufferSize, inputConfigType, &inputTaskHandle, &outputTaskHandle, &trigTaskHandle));
}
else
{
//First let's configure out input voltage.
outputVoltage = stopParam;
//Next set up our output buffer control. This tells the thread running the output code when to switch buffers.
currentOutputBuffer = bufferControl->outputBuffer = wave;
bufferControl->outputBufferSize = &outputBufferSize;
//And then configure out IO device for SR mode.
DAQmxErrChk (initSR(inputChan, outputChan, refChan, trigChan, useRefChan, useTrigChan, bufferControl, readInBufferSize, freqOut, maxRateOut, maxRateIn, inputVoltage, outputVoltage, inputConfigType, &inputTaskHandle, &outputTaskHandle, &trigTaskHandle));
}
//Now let's prep the plots.
//First the input voltage plot
if (canUpdate(panel, magChart))
{
//SetAxisRange(panel, magChart, VAL_NO_CHANGE, 0, 1, VAL_MANUAL, 0, inputVoltage);
DeleteGraphPlot(*panel, magChart, -1, VAL_IMMEDIATE_DRAW);
}
//Then the phase plot.
if (canUpdate(panel, phaseChart))
{
//SetAxisRange(panel, signalsPlot, VAL_NO_CHANGE, 0, 1, VAL_MANUAL, -1.0 * inputVoltage, inputVoltage);
DeleteGraphPlot(*panel, phaseChart, -1, VAL_IMMEDIATE_DRAW);
}
//Let's set our mag/phase data plot axis type.
if (stepType == 0)
{
//If we're in steps per decade mode set the x axis to logrithmic.
if (canUpdate(panel, magChart))
SetCtrlAttribute(*panel, magChart, ATTR_XMAP_MODE, VAL_LOG);
if (canUpdate(panel, phaseChart))
SetCtrlAttribute(*panel, phaseChart, ATTR_XMAP_MODE, VAL_LOG);
}
else
{
//Otherwise let's set our plot to linear mode.
if (canUpdate(panel, magChart))
SetCtrlAttribute(*panel, magChart, ATTR_XMAP_MODE, VAL_LINEAR);
if (canUpdate(panel, magChart))
SetCtrlAttribute(*panel, phaseChart, ATTR_XMAP_MODE, VAL_LINEAR);
}
//Finally let's process draw events and we're ready to go.
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Running");
ProcessDrawEvents();
freq = 0;
*vnaRunning = 1;
do
{
//Before we begin let's update our status message.
if (canUpdate(panel, freqLabel))
{
if (measurementType == FREQ_MEASUREMENT)
sprintf(freqStr, "%s: %0.2f %s", measurement, (*vnaFreqData)[freq], unit);
else
sprintf(freqStr, "%s: %0.2f %s", measurement, (*vnaOutputData)[freq], unit);
SetCtrlVal(*panel, freqLabel, freqStr);
}
//Now let's see if we're at the point where we want to start averaging.
//Basically we can turn it on only past a certain frequency if need be.
if (!(*requestStop))
{
if ((*vnaFreqData)[freq] >= avgStartFreq)
numTrials = trialsToRun;
else
numTrials = 1;
}
trialCount = 0;
//Now let's make sure we set our SR output buffer flag.
//This is so if we're in SR mode we won't be writing a new buffer when we average more than one cycle together.
updateSRBuffer = 1;
//Here begins the loop for averaging when we're averaging read-in voltages together.
//This is for the SR code. Since we start reading at arbitrary points there we have no
//choice but to have this approach. Maybe if the SR code will be rewritten to be triggered
//this will change.
do
{
if (!(*requestStop))
{
//Next let's zero our averages array.
for (int i = 0; i < dataBufferSize; i++)
averageArray[i] = 0.0;
//Reset our input array. This eliminates potential issues.
//This code can probably be removed for optimization since we're not reading past the read-in sample anyway.
/*for (i = 0; i < dataBufferSize; i++)
data[i]= 0;*/
//First things first, let's figure out our experiment parameters, the frequency, output voltage, etc.
//This will of course depend on our measurement type.
if (measurementType == FREQ_MEASUREMENT)
{
freqOut = (*vnaFreqData)[freq];
(*vnaOutputData)[freq] = outputVoltage;
}
else if (measurementType == VOLT_MEASUREMENT)
{
(*vnaFreqData)[freq] = freqOut;
}
//Now let's calculate the number of cycles we'll be inputting/outputting.
cyclesOut = getCyclesOut(freqOut);
//Next let's compute the current number of output and input samples for the given frequency.
//Recomputing this stuff is unnecessary for the SR code, but it doesn't hurt either.
getSamplesPerCycle(freqOut, maxRateOut, &samplesPerCycle, &adjustedSamplesPerCycle);
getSamplesPerCycle(freqOut, maxRateIn, &samplesPerCycleIn, &adjustedSamplesPerCycleIn);
samplesToWrite = getBufferSize(cyclesOut, adjustedSamplesPerCycle) + 1;
samplesToRead = getBufferSize(cyclesOut, adjustedSamplesPerCycleIn) + 1;
//Next let's figure out our input and output rates.
//Basically if we had to adjust the input or output buffers we have to change the clocks to that when the hardware produces the wave
//it is at the desired frequency.
rateIn = getAdjustedDeviceRate (freqOut, maxRateIn, samplesPerCycleIn, adjustedSamplesPerCycleIn);
rateOut = getAdjustedDeviceRate (freqOut, maxRateOut, samplesPerCycle, adjustedSamplesPerCycle);
//Next let's generate our data
genPhase = 0;
genAmp = (*vnaOutputData)[freq];
SineWave (outputBufferSize, (*vnaOutputData)[freq], 1.0 / adjustedSamplesPerCycle, &genPhase, currentOutputBuffer);
//And set up our data plot and x array for graphing.
//SetAxisRange(*panel, signalsPlot, VAL_NO_CHANGE, 0, 0, VAL_MANUAL, -3.0 * (*vnaMagData)[freq], 3.0 * (*vnaMagData)[freq]);
buildLinearArray((1.0 / rateIn), 0, xVals, readInBufferSize);
//Now let's get our data.
do
{
if (measurementType == FREQ_MEASUREMENT)
{
//If we're doing a frequency sweep let' act accordingly
DAQmxErrChk(getVNAPoint((*vnaFreqData)[freq], rateIn, rateOut, readInBufferSize, dataBufferSize, outputBufferSize, inputVoltage, outputVoltage, inputTaskHandle, outputTaskHandle, useRefChan, trigTaskHandle, useTrigChan, triggerType, fitFromEnd, samplesToRead, samplesToWrite, &numRead, currentOutputBuffer, data));
}
else
{
//Otherwise if we're doing a voltage sweep let's act accordinly
DAQmxErrChk(getSRPoint(bufferControl, freq + trialCount, updateSRBuffer, freqOut, dataBufferSize, samplesToRead, samplesToWrite, inputTaskHandle, outputTaskHandle, useRefChan, trigTaskHandle, useTrigChan, triggerType, settleTime, currentOutputBuffer, data, &numRead, requestStop));
currentOutputBuffer = findNewSRWaveAddr(bufferControl, useTrigChan);
updateSRBuffer = 0;
}
//Now let's add our latest results to the averages array.
for (int j = 0; j < dataBufferSize; j++)
averageArray[j] += (((measurementType == FREQ_MEASUREMENT) ? avgMultiplier : 1) * data[j]);
//Finally let's process our system events. Collecting data can be time consuming and we need to see if the user asked the program to do something else.
ProcessSystemEvents();
//Let's see if the user asked to exit the loop.
if (*requestStop)
break;
//This would be a good place to increment the trial count varibale.
trialCount++;
}
while (measurementType == FREQ_MEASUREMENT && trialCount < numTrials);
}
if (!(*requestStop))
{
double ampVar;
double phaseVar;
//Next let's fit our current averages array and build our fitted function.
computeWaveParameters(numRead, useRefChan, cyclesOut, freqOut, rateIn, adjustedSamplesPerCycleIn, fitFromEnd, &computedSamplesPerCycleIn, averageArray, &dataToFit, &refDataToFit, &Var, &phaseVar, &samplesToFit, &mainDataPhase, &refDataPhase);
(*vnaMagData)[freq] += ampVar;
(*vnaPhaseData)[freq] += phaseVar;
}
//Now that we have our final waveform, let's perform some processing on it to get a better measurement.
/* if (!(*requestStop))
{
double min;
double max;
double offset;
ssize_t minpos;
ssize_t maxpos;
//First let's adjust for constant DC offset.
MaxMin1D(data, numRead, &max, &maxpos, &min, &minpos);
offset = (max + min) / 2.0;
for (int i = 0; i < dataBufferSize; i++)
averageArray[i] -= offset;
//Next let's fit our current averages array and build our fitted function.
computeWaveParameters(numRead, useRefChan, cyclesOut, freqOut, rateIn, adjustedSamplesPerCycleIn, fitFromEnd, &computedSamplesPerCycleIn, averageArray, &dataToFit, &refDataToFit, &((*vnaMagData)[freq]), &((*vnaPhaseData)[freq]), &samplesToFit, &mainDataPhase, &refDataPhase);
SineWave(readInBufferSize, (*vnaMagData)[freq], 1.0 / computedSamplesPerCycleIn, &mainDataPhase, fittedFunction);
//And then plot our intermediate results.
if (canUpdate(panel, signalsPlot))
{
DeleteGraphPlot(*panel, signalsPlot, -1, VAL_IMMEDIATE_DRAW);
plotAttr = PlotXY (*panel, signalsPlot, xVals, dataToFit, samplesToFit, VAL_DOUBLE, VAL_DOUBLE,VAL_FAT_LINE ,VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
SetPlotAttribute(*panel, signalsPlot, plotAttr,ATTR_PLOT_LG_TEXT, "Data");
plotAttr = PlotXY (*panel, signalsPlot, xVals, fittedFunction, samplesToFit, VAL_DOUBLE, VAL_DOUBLE,VAL_THIN_LINE ,VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
SetPlotAttribute(*panel, signalsPlot, plotAttr,ATTR_PLOT_LG_TEXT, "Fitted");
}
} */
if ((*requestStop))
break;
} while (measurementType == VOLT_MEASUREMENT && trialCount < numTrials);
if (measurementType == VOLT_MEASUREMENT)
{
(*vnaMagData)[freq] = (*vnaMagData)[freq] / numTrials;
(*vnaPhaseData)[freq] = (*vnaPhaseData)[freq] / numTrials;
}
if (!(*requestStop))
{
SineWave(readInBufferSize, (*vnaMagData)[freq], 1.0 / computedSamplesPerCycleIn, &mainDataPhase, fittedFunction);
//And then plot our intermediate results.
if (canUpdate(panel, signalsPlot))
{
DeleteGraphPlot(*panel, signalsPlot, -1, VAL_IMMEDIATE_DRAW);
plotAttr = PlotXY (*panel, signalsPlot, xVals, dataToFit, samplesToFit, VAL_DOUBLE, VAL_DOUBLE,VAL_FAT_LINE ,VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
SetPlotAttribute(*panel, signalsPlot, plotAttr,ATTR_PLOT_LG_TEXT, "Data");
plotAttr = PlotXY (*panel, signalsPlot, xVals, fittedFunction, samplesToFit, VAL_DOUBLE, VAL_DOUBLE,VAL_THIN_LINE ,VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
SetPlotAttribute(*panel, signalsPlot, plotAttr,ATTR_PLOT_LG_TEXT, "Fitted");
}
}
//Next let's find our true input voltage given our amplifier and divier settings.
//Let's only run all this stuff though if a stop hasn't been requested.
if (!(*requestStop))
{
(*vnaOutputData)[freq] = findTrueOutputVoltage((*vnaOutputData)[freq], useExternalAmp, ampGain);
if (measurementType == FREQ_MEASUREMENT)
{
(*vnaMagData)[freq] = findTrueInputVoltage((*vnaMagData)[freq], (*vnaFreqData)[freq], useDivider, dividerRatio,&cal);
(*vnaPhaseData)[freq] = findTrueInputPhase((*vnaPhaseData)[freq], (*vnaFreqData)[freq], &cal);
}
else
{
(*vnaMagData)[freq] = findTrueInputVoltage((*vnaMagData)[freq], freqOut, useDivider, dividerRatio,&cal);
(*vnaPhaseData)[freq] = findTrueInputPhase((*vnaPhaseData)[freq], freqOut, &cal);
}
//Now let's plot what we have of the bode plots so far.
if (canUpdate(panel, magChart))
{
if (measurementType == FREQ_MEASUREMENT)
{
//SetAxisRange(panel, magChart,
PlotXY (*panel, magChart, *vnaFreqData, *vnaMagData, freq + 1, VAL_DOUBLE, VAL_DOUBLE, VAL_FAT_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
}
else
PlotXY (*panel, magChart, *vnaOutputData, *vnaMagData, freq + 1, VAL_DOUBLE, VAL_DOUBLE, VAL_FAT_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
}
if (canUpdate(panel, phaseChart))
{
if (measurementType == FREQ_MEASUREMENT)
PlotXY (*panel, phaseChart, *vnaFreqData, *vnaPhaseData, freq + 1, VAL_DOUBLE, VAL_DOUBLE, VAL_FAT_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
else
PlotXY (*panel, phaseChart, *vnaOutputData, *vnaPhaseData, freq + 1, VAL_DOUBLE, VAL_DOUBLE, VAL_FAT_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_WHITE);
}
//Lets wait for a few ms to let the user look at the plot.
Delay(delayTime);
}
//Finally let's see if we're done.
if (*requestStop || (freq >= (totalSteps - 1)))
{
//If we are we set our running flag.
*vnaRunning = 0;
}
else
{
ProcessSystemEvents();
freq++;
}
} while (*vnaRunning);
//Let's reset the chan to 0 now that we're done running.
DAQmxErrChk(zeroOutputChan(outputTaskHandle, outputBufferMultiplier, inputTaskHandle, useRefChan, trigTaskHandle, useTrigChan, triggerType, maxRateIn, maxRateOut));
//Now let's simply update our status and we're done.
if (canUpdate(panel, status))
SetCtrlVal(*panel, status, "Status: Idle");
Error:
if( inputTaskHandle!= 0 )
StopAndClear_MultiFunctionSynchAIAO(inputTaskHandle);
if( outputTaskHandle!= 0 )
StopAndClear_MultiFunctionSynchAIAO(outputTaskHandle);
if( trigTaskHandle != 0 )
StopAndClear_MultiFunctionSynchAIAO(trigTaskHandle);
if( wave )
free(wave);
if( data )
free(data);
if( averageArray )
free(averageArray);
if( fittedFunction )
free(fittedFunction);
if( xVals )
free(xVals);
return error;
}
int canUpdate(int *panel, element)
{
if (panel && *panel >= 0 && element > 0)
return 1;
else
return 0;
}
void determineElementUpdateStatus (int panel, int element, int *canUpdate)
{
if (panel && canUpdate)
{
if (panel >= 0 && element > 0)
*canUpdate = 1;
else
*canUpdate = 0;
}
}
int prepVNAData(int numDataPoints, double **vnaFreqData, double **vnaOutputData, double **vnaMagData, double **vnaPhaseData, int *vnaArraySize, int *vnaDataPoints)
{
//First let's make sure we got everything.
if(!vnaFreqData || !vnaMagData || !vnaPhaseData || !vnaArraySize || !vnaDataPoints)
return -1;
//In order to save time we're going to reallocate the data array only if the number of new points is greater than the current number of data points.
if(numDataPoints > *vnaArraySize)
{
//If it is let's delete the current arrays
if (*vnaFreqData)
{
free(*vnaFreqData);
*vnaFreqData = NULL;
}
if (*vnaOutputData)
free(*vnaOutputData);
if (*vnaMagData)
free(*vnaMagData);
if (*vnaPhaseData)
free(*vnaPhaseData);
//Next let's reallocate our arrays.
*vnaFreqData = (double*) malloc(numDataPoints * sizeof(double));
*vnaOutputData = (double*) malloc(numDataPoints * sizeof(double));
*vnaMagData = (double*) malloc(numDataPoints * sizeof(double));
*vnaPhaseData = (double*) malloc(numDataPoints * sizeof(double));
//And then allocate a new one.
if (!(*vnaFreqData) || !(*vnaOutputData) || !(*vnaMagData) || !(*vnaPhaseData))
{
//If it failed let's update the size variables.
*vnaDataPoints = *vnaArraySize = 0;
//And let the user know.
return -1;
}
//If it succeeded let's update the number of points variable.
*vnaDataPoints = *vnaArraySize = numDataPoints;
}
else
{
//Otherwise all we need to do is just set the new number of data points variable.
*vnaDataPoints = numDataPoints;
}
//Let's reset the array.
for (int i = 0; i < *vnaArraySize; i++)
{
(*vnaFreqData)[i] = 0;
(*vnaOutputData)[i] = 0;
(*vnaMagData)[i] = 0;
(*vnaPhaseData)[i] = 0;
}
return 0;
}
unsigned int buildStepArray (double startFreq, double stopFreq, int stepType, unsigned short stepsPerUnit, double *stepArray)
{
//Declarations
unsigned int totalSteps = 0;
double stepSize;
double logVal;
double temp;
int i;
uInt8 genArray;
//First let's set up our environment variables.
//Let's see if we need to generate our array or if we're just counting the number of array entry points we'll need.
if (stepArray)
genArray = 1;
else
genArray = 0;
//Next let's make sure startFreq <= stopFreq
if (startFreq > stopFreq)
{
temp = startFreq;
startFreq = stopFreq;
stopFreq = temp;
}
//Now let's figure out the number of data points we're going to take.
if (startFreq == stopFreq)
{
//If they're the same number let's just do one.
totalSteps = 1;
if (genArray)
stepArray[0] = startFreq;
}
else if (stepType == 0)
{
//If we're in steps per decade mode.
//We need to figure out the number of complete decades and multiply it by the steps per decade.
logVal = log10(stopFreq / startFreq);
totalSteps += stepsPerUnit * ((int) logVal);
//Next we need to do something a bit bizzare.
//We need to see if the stop frequency is not the start frequency times some power of ten.
//This is important because we may need to take additional steps to get to the end frequency.
//We do this by seeing if the power and the truncated power are the same number.
if (logVal != floor(logVal))
{
//If it's not, let's figure out the step size for this decade and compute how many steps it takes to get from the start of the ocate
//to the stop frequency.
temp = startFreq * pow(10.0, floor(logVal));
stepSize = (startFreq * pow(10.0, floor(logVal) + 1.0) - temp) / stepsPerUnit;
//We need to divide the range of the by the step size to get the last few steps.
//We need to take the ceiling of this division because any fraction means we have an additional step, the end frequency.
temp = (stopFreq - temp) / stepSize;
totalSteps += (int) ceil(temp);
}
//This is for the upper bound.
totalSteps++;
//Next let's generate the array if we were passed one.
if(genArray)
{
double minLog = log10(startFreq);
double maxLog = log10(stopFreq);
stepSize = (maxLog - minLog) / (totalSteps - 1);
for (i = 0; i < (totalSteps - 1); i++)
{
stepArray[i] = pow(10.0, minLog + (i * stepSize));
}
stepArray[totalSteps - 1] = stopFreq;
}
}
else if (stepType == 1)
{
//Else if we're in steps per octave mode. The plan is largely the same here, only with powers of 2.
logVal = log(stopFreq / startFreq) / log(2.0);
totalSteps += stepsPerUnit * ((int) logVal);
if (logVal != floor(logVal))
{
temp = startFreq * pow(2.0, floor(logVal));
stepSize = (startFreq * pow(2.0, floor(logVal) + 1.0) - temp) / stepsPerUnit;
temp = (stopFreq - temp) / stepSize;
totalSteps += (int)ceil(temp);
}
totalSteps++;
if (genArray)
{
double minLog = log10(startFreq) / log10(2.0);
double maxLog = log10(stopFreq) / log10(2.0);
stepSize = (maxLog - minLog) / (totalSteps - 1);
for (i = 0; i < totalSteps - 1; i++)
{
stepArray[i] = pow(2.0, minLog + (i * stepSize));
//stepArray[i] = (startFreq + stepSize * (i % stepsPerUnit)) * pow(2.0, floor(i / stepsPerUnit));
}
stepArray[totalSteps - 1] = stopFreq;
}
}
else
{
//Otherwise we're doing evenly spaced steps. Let's set our plot to linear mode.
totalSteps = stepsPerUnit;
if (genArray)
{
stepSize = (stopFreq - startFreq) / (stepsPerUnit - 1);
for (i = 0; i < stepsPerUnit; i++)
{
stepArray[i] = startFreq + (i * stepSize);
}
}
}
return totalSteps;
}
int initVNA(char *inputChan, char *outputChan, char *refChan, char *trigChan, int useRefChan, int useTrigChan, double freqOut, double maxRateOut, double maxRateIn, double inputVoltage, double outputVoltage, unsigned int dataBufferSize, unsigned int outputBufferSize, unsigned int inputConfigType, TaskHandle *inputTaskHandle, TaskHandle *outputTaskHandle, TaskHandle *trigTaskHandle)
{
int inputType;
int outputType;
outputType = inputType = DAQmx_Val_FiniteSamps;
return (ConfigIO(inputChan, refChan, outputChan, trigChan, useRefChan, 1, useTrigChan, 1, 1, -inputVoltage, -outputVoltage, inputVoltage, outputVoltage, maxRateIn, maxRateOut, inputTaskHandle, outputTaskHandle, trigTaskHandle, inputType, outputType, dataBufferSize, outputBufferSize, inputConfigType));
}
int getVNAPoint(double freqOut, double rateIn, double rateOut, unsigned int readInBufferSize, unsigned int dataBufferSize, unsigned int outputBufferSize, double inputVoltage, double outputVoltage, TaskHandle inputTaskHandle, TaskHandle outputTaskHandle, uInt8 useRefChan, TaskHandle triggerTaskHandle, uInt8 useTrigger, int triggerType, int fitFromEnd, unsigned int samplesToRead, unsigned int samplesToWrite, unsigned int *numRead, float64 *wave, float64 *data)
{
//Declarations
int32 error = 0;
int inputType;
int outputType;
double zeroOutBuffer[2] = {0.0, 0.0};
double zeroInBuffer[4];
int numZerosRead;
//Let's make sure we have pointers for the data we'll be return.
if (!numRead || !wave || !data)
return -1;
//Next let's set our in/out types.
outputType = inputType = DAQmx_Val_FiniteSamps;
//Now let's configure the clock on our channels
DAQmxErrChk (ReconfigureChannelClock(rateIn, inputTaskHandle, inputType, samplesToRead));
DAQmxErrChk (ReconfigureChannelClock(rateOut, outputTaskHandle, outputType, samplesToWrite));
//DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(inputTaskHandle, DAQmx_Val_Acquired_Into_Buffer, (uInt32) rateIn / 4, 0, updateSignalPlot, NULL));
//And prime the output
DAQmxErrChk (Write_MultiFunctionSynchAIAO(outputTaskHandle, wave, samplesToWrite));
DAQmxErrChk (Start_MultiFunctionSynchAIAO(outputTaskHandle));
//Now let's read in the data.
DAQmxErrChk (Start_MultiFunctionSynchAIAO(inputTaskHandle));
//Next we need to see if we're using an external channel as a trigger.
if (useTrigger)
DAQmxErrChk (setTrigger(triggerTaskHandle, triggerType));
DAQmxErrChk (Read_MultiFunctionSynchAIAO(inputTaskHandle, samplesToRead, data, dataBufferSize, numRead, DAQmx_Val_GroupByChannel));
//Now let's stop reading.
DAQmxErrChk (Stop_MultiFunctionSynchAIAO(inputTaskHandle));
DAQmxErrChk (Stop_MultiFunctionSynchAIAO(outputTaskHandle));
//And reset our trigger.
if (useTrigger)
DAQmxErrChk (resetTrigger(triggerTaskHandle, triggerType));
//Next let's reset the output, force the voltage to be 0.
//This will reduce problems on the next cycle.
DAQmxErrChk (ReconfigureChannelClock(rateIn, inputTaskHandle, inputType, 2));
DAQmxErrChk (ReconfigureChannelClock(rateOut, outputTaskHandle, outputType, 2));
DAQmxErrChk (Write_MultiFunctionSynchAIAO(outputTaskHandle, zeroOutBuffer, 2));
DAQmxErrChk (Start_MultiFunctionSynchAIAO(outputTaskHandle));
DAQmxErrChk (Start_MultiFunctionSynchAIAO(inputTaskHandle));
if (useTrigger)
DAQmxErrChk (setTrigger(triggerTaskHandle, triggerType));
DAQmxErrChk (Read_MultiFunctionSynchAIAO(inputTaskHandle, 2, zeroInBuffer, 4, &numZerosRead, DAQmx_Val_GroupByChannel));
DAQmxErrChk (Stop_MultiFunctionSynchAIAO(inputTaskHandle));
DAQmxErrChk (Stop_MultiFunctionSynchAIAO(outputTaskHandle));
//DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(inputTaskHandle, DAQmx_Val_Acquired_Into_Buffer, (uInt32) rateIn / 4, 0, NULL, NULL));
if (useTrigger)
DAQmxErrChk (resetTrigger(triggerTaskHandle, triggerType));
Error:
return error;
}
int initSR(char *inputChan, char *outputChan, char *refChan, char *trigChan, int useRefChan, int useTrigChan, BufferControl *bufferControl, unsigned int dataBufferSize, double freqOut, double maxRateOut, double maxRateIn, double inputVoltage, double outputVoltage, int inputConfigType, TaskHandle *inputTaskHandle, TaskHandle *outputTaskHandle, TaskHandle *trigTaskHandle)
{
//Declarations
int32 error = 0;
double samplesPerCycle;
double adjustedSamplesPerCycle;
double samplesPerCycleIn;
double adjustedSamplesPerCycleIn;
double rateIn;
double rateOut;
unsigned int samplesToWrite;
unsigned int samplesToRead;
unsigned int cyclesOut;
double genPhase;
int inputType = DAQmx_Val_FiniteSamps;
int outputType = DAQmx_Val_ContSamps;
//First let's make sure we got everything.
if (!bufferControl || !inputTaskHandle || !outputTaskHandle || (useTrigChan && !trigTaskHandle))
return -1;
cyclesOut = getCyclesOut(freqOut);
//It's important that we get the details right immidiately, we're not going to be reconfiguring the output channel once it's started.
//So let's do a few calculations right now and configure out channels accordingly.
getSamplesPerCycle(freqOut, maxRateOut, &samplesPerCycle, &adjustedSamplesPerCycle);
getSamplesPerCycle(freqOut, maxRateIn, &samplesPerCycleIn, &adjustedSamplesPerCycleIn);
samplesToWrite = getBufferSize(cyclesOut, adjustedSamplesPerCycle) + 1;
samplesToRead = getBufferSize(cyclesOut, adjustedSamplesPerCycleIn) + 1;
rateIn = getAdjustedDeviceRate (freqOut, maxRateIn, samplesPerCycleIn, adjustedSamplesPerCycleIn);
rateOut = getAdjustedDeviceRate (freqOut, maxRateOut, samplesPerCycle, adjustedSamplesPerCycle);
//First let's set up our global flags.
bufferControl->currentBuffer = 0;
bufferControl->switchBufferAfterLastSample = 0;
//Now let's set up the IO channels
DAQmxErrChk (ConfigIO(inputChan, refChan, outputChan, trigChan, useRefChan, useTrigChan, useTrigChan, 1, 0, -inputVoltage, -outputVoltage, inputVoltage, outputVoltage, rateIn, rateOut, inputTaskHandle, outputTaskHandle, trigTaskHandle, inputType, outputType, dataBufferSize, *(bufferControl->outputBufferSize), inputConfigType));
//Now let's prep our buffer switching function.
DAQmxErrChk (DAQmxRegisterEveryNSamplesEvent(*outputTaskHandle, DAQmx_Val_Transferred_From_Buffer, *(bufferControl->outputBufferSize) + 1, 0, updateOutputBuffer, bufferControl));
Error:
return error;
}
float64* findNewSRWaveAddr(BufferControl *bufferControl, int useTrigger)
{
//First we need to know if the output is using the first buffer or the second buffer.
int bufferShift = !(bufferControl->currentBuffer);
//Next we need to know if we're using a trigger channel, which is an additional buffer of data.
//If we are, let's adjust our output position accordingly.
if (useTrigger)
bufferShift *= 2;
//Our wave address is nothing more than our buffer start size plus our buffer size.
return ((bufferControl->outputBuffer)) + bufferShift * (*(bufferControl->outputBufferSize));
}
int getSRPoint(BufferControl *bufferControl, int run, int updateBuffer, double freqOut, unsigned int dataBufferSize, unsigned int samplesToRead, unsigned int samplesToWrite, TaskHandle inputTaskHandle, TaskHandle outputTaskHandle, uInt8 useRefChan, TaskHandle triggerTaskHandle, uInt8 useTrigger, int triggerType, double settleTime, float64 *wave, float64 *data, int *numRead, int *requestStop)
{
//To do:
// 8. Incorporate external digital trigger.
//Declarations
int32 error = 0;
int inputType;
int outputType;
double rateIn;
double rateOut;
double mainDataPhase;
double refDataPhase;
double endTime;
//Let's make sure we have pointers for the data we'll be return.
if (!bufferControl || !wave || !data)
return -1;
if (run == 0)
{
//Let's write the data to our output channel.
DAQmxErrChk (Write_MultiFunctionSynchAIAO(outputTaskHandle, wave, samplesToWrite));
//Let's just start the output
DAQmxErrChk (Start_MultiFunctionSynchAIAO(outputTaskHandle));
//Now that our output has started let's give our system a chance to settle.
waitSettleTime(settleTime, requestStop);
}
else if (updateBuffer)
{
//Otherwise let's just set up our buffer and raise the flag to switch buffers.
bufferControl->newDataAddress = wave;
//Now let's make sure our other thread didn't encounter any errors thus far.
DAQmxErrChk(bufferControl->error);
//We're ready to go. Let's raise the flag for the switch.
bufferControl->switchBufferAfterLastSample = 1;
//Let's wait until our buffers are done switching.
//We'll know it's done when the switch buffer flag is reset.
while (bufferControl->switchBufferAfterLastSample == 1)
{
Delay(0.1);
}
//Now that our buffers have switched let's give our output a chance to settle.
waitSettleTime(settleTime, requestStop);
}
//Now let's start the input task to read in the data.
DAQmxErrChk (Start_MultiFunctionSynchAIAO(inputTaskHandle));
//Next we need to see if we're using an external channel as a trigger to trigger the input.
if (useTrigger)
DAQmxErrChk (setTrigger(triggerTaskHandle, triggerType));
//Now let's read in the data.
DAQmxErrChk (Read_MultiFunctionSynchAIAO(inputTaskHandle, samplesToRead, data, dataBufferSize, numRead, DAQmx_Val_GroupByChannel));
//Now let's stop reading.
DAQmxErrChk (Stop_MultiFunctionSynchAIAO(inputTaskHandle));
//And reset our trigger.
if (useTrigger)
DAQmxErrChk (resetTrigger(triggerTaskHandle, triggerType));
Error:
return error;
}
int waitSettleTime(double settleTime, int *requestStop)
{
double startTime = Timer();
double currentTime = Timer();
while ((currentTime - startTime) < settleTime)
{
Delay(0.1);