Skip to content

Commit f122c18

Browse files
authored
Merge pull request #78 from shuchitak/bugfix/fs_deviation_increase_precision
Increase fFsRatioDeviation precision in the C model
2 parents 3ba8658 + 3134014 commit f122c18

3 files changed

Lines changed: 53 additions & 53 deletions

File tree

CHANGELOG.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ lib_src change log
1212
* CHANGED: Golden reference test signals now generated automatically by CI
1313
* RESOLVED: Linker warning on channel ends
1414
* REMOVED: AN00231 ASRC App Note. See github.com/xmos/sln_voice/examples
15+
* CHANGED: Increased precision of the fFsRatioDeviation used in the C emulator
16+
from float to double
1517

1618
* Changes to dependencies:
1719

tests/asrc_test/dut/asrc_test.xc

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ int uiInFs = -1;
3333
int uiOutFs = -1;
3434

3535
//Global deviation ratio from nominal sample rate in/out ratio. Initialise to invalid.
36-
float fFsRatioDeviation = -1.0000;
36+
double fFsRatioDeviation = (double)-1.0000;
3737

3838
const int sample_rates[] = {44100, 48000, 88200, 96000, 176400, 192000};
3939

@@ -67,13 +67,13 @@ void dsp_slave(chanend c_dsp)
6767
asrc_ctrl[ui].piStack = asrc_stack[ui];
6868
asrc_ctrl[ui].piADCoefs = asrc_adfir_coefs.iASRCADFIRCoefs;
6969
}
70-
70+
7171
/*
7272
// Update Fs Ratio
7373
for(int i = 0; i < ASRC_N_CHANNELS; i++)
7474
{
7575
// Make Fs Ratio deviate
76-
asrc_ctrl[i].uiFsRatio = (unsigned int)(asrc_ctrl[i].uiFsRatio * fFsRatioDeviation);
76+
asrc_ctrl[i].uiFsRatio = (unsigned int)((double)asrc_ctrl[i].uiFsRatio * fFsRatioDeviation);
7777
if(ASRC_update_fs_ratio(&asrc_ctrl[i]) != ASRC_NO_ERROR)
7878
{
7979
printf("Error updating ASRC fs ratio\n");
@@ -103,7 +103,7 @@ void dsp_slave(chanend c_dsp)
103103
}
104104

105105
n_samps_in_tot += ASRC_N_IN_SAMPLES;
106-
106+
107107
c_dsp <: n_samps_out; //Send number of samples to receive
108108

109109
for(unsigned uj = 0; uj < n_samps_out; uj++)
@@ -121,7 +121,7 @@ void dsp_slave(chanend c_dsp)
121121
unsigned OutFs = sr_in_out_new & 0xffff;
122122

123123
unsigned nominal_FsRatio = asrc_init(InFs, OutFs, asrc_ctrl, ASRC_CHANNELS_PER_INSTANCE, ASRC_N_IN_SAMPLES, ASRC_DITHER_SETTING);
124-
124+
125125
sr_in_out = sr_in_out_new;
126126
printf("DSP init Initial nominal_FsRatio=%d, SR in=%d, SR out=%d\n", nominal_FsRatio, InFs, OutFs);
127127
}
@@ -130,7 +130,7 @@ void dsp_slave(chanend c_dsp)
130130
}
131131
}
132132

133-
void dsp_mgr(chanend c_dsp[], float fFsRatioDeviation){
133+
void dsp_mgr(chanend c_dsp[], double fFsRatioDeviation){
134134

135135
FILE * movable InFileDat[ASRC_N_CHANNELS];
136136
FILE * movable OutFileDat[ASRC_N_CHANNELS];
@@ -140,11 +140,11 @@ void dsp_mgr(chanend c_dsp[], float fFsRatioDeviation){
140140
unsigned iEndOfFile = 0;
141141
unsigned int sr_in_out = uiInFs << 16 | uiOutFs ; //Input fs in upper 16bits and Output fs in lower 16bits
142142
unsigned FsRatio = (unsigned) (((unsigned long long)sample_rates[uiInFs] * (unsigned long long)(1<<28)) / (unsigned long long)sample_rates[uiOutFs]);
143-
144-
FsRatio = (unsigned int)((float)FsRatio * fFsRatioDeviation); //Ensure is precisely the same as golden value complete with trucation due to 32b float
143+
144+
FsRatio = (unsigned int)((double)FsRatio * fFsRatioDeviation); //Ensure is precisely the same as golden value complete with trucation due to 32b float
145145
printf("Adjusted FsRatio dsp_mgr = %d, 0x%x\n", FsRatio, FsRatio);
146146

147-
147+
148148

149149
for (int i=0; i<ASRC_N_CHANNELS; i++)
150150
unsafe
@@ -164,7 +164,7 @@ void dsp_mgr(chanend c_dsp[], float fFsRatioDeviation){
164164

165165

166166
while(!iEndOfFile)
167-
{
167+
{
168168
for (unsigned j=0; j<ASRC_N_INSTANCES; j++) {
169169
c_dsp[j] <: sr_in_out;
170170
c_dsp[j] <: FsRatio;
@@ -180,21 +180,21 @@ void dsp_mgr(chanend c_dsp[], float fFsRatioDeviation){
180180
printf("EOF\n");
181181
}
182182
c_dsp[j] <: samp; //Send the sample
183-
}
183+
}
184184
}
185-
count_in += ASRC_N_IN_SAMPLES;
185+
count_in += ASRC_N_IN_SAMPLES;
186186

187187

188188
unsigned n_samps;
189-
for (unsigned j=0; j<ASRC_N_INSTANCES; j++)
189+
for (unsigned j=0; j<ASRC_N_INSTANCES; j++)
190190
{
191191
c_dsp[j] :> n_samps; //Get number of samps to receive
192192
}
193193

194-
for(unsigned i = 0; i < n_samps * ASRC_CHANNELS_PER_INSTANCE; i++)
194+
for(unsigned i = 0; i < n_samps * ASRC_CHANNELS_PER_INSTANCE; i++)
195195
{
196196
int samp;
197-
for (unsigned j=0; j<ASRC_N_INSTANCES; j++)
197+
for (unsigned j=0; j<ASRC_N_INSTANCES; j++)
198198
{
199199
unsigned file_index = (i * ASRC_N_INSTANCES + j) % ASRC_N_CHANNELS;
200200
c_dsp[j] :> samp; //Get samples
@@ -232,7 +232,7 @@ void ShowUsage()
232232
" -g Output sample rate (44100 - 192000)\n\n"
233233
" -n Number of input samples (all channels) to process\n\n"
234234
);
235-
235+
236236
exit(0);
237237
}
238238

@@ -268,13 +268,13 @@ void ParseCmdLine(char *input, char * unsafe * argv, int ui)
268268
{
269269
switch (*input)
270270
{
271-
271+
272272
case 'h':
273273
case 'H':
274274
ShowUsage();
275275
exit(0);
276276
break;
277-
277+
278278
case 'i':
279279
case 'I':
280280
for (int i=0; i<ASRC_N_CHANNELS; i++)
@@ -305,7 +305,7 @@ void ParseCmdLine(char *input, char * unsafe * argv, int ui)
305305
case 'g':
306306
case 'G':
307307
uiOutFs = (unsigned int)(atoi((char *)argv[ui + 1]));
308-
uiOutFs = samp_rate_to_code(uiOutFs);
308+
uiOutFs = samp_rate_to_code(uiOutFs);
309309
if((uiOutFs < ASRC_FS_MIN) || (uiOutFs > ASRC_FS_MAX))
310310
{
311311
printf("ERROR: invalid frequency index %d\n", uiOutFs);
@@ -315,7 +315,7 @@ void ParseCmdLine(char *input, char * unsafe * argv, int ui)
315315

316316
case 'e':
317317
case 'E':
318-
fFsRatioDeviation = (float)(atof((char *)argv[ui + 1]));
318+
fFsRatioDeviation = (double)(atof((char *)argv[ui + 1]));
319319
//Note no check. This is done at run-time.
320320
break;
321321

@@ -346,14 +346,14 @@ void ParseCmdLine(char *input, char * unsafe * argv, int ui)
346346
}
347347
}
348348

349-
int main(int argc, char * unsafe argv[])
349+
int main(int argc, char * unsafe argv[])
350350
{
351351
int ui;
352352

353353
printf("Running ASRC test simulation \n");
354354
if (argc <= 1 ) ShowUsage();
355355

356-
//Parse command line arguments
356+
//Parse command line arguments
357357
for (ui = 1; ui < (unsigned int)argc; ui++)
358358
unsafe{
359359
if (*(argv[ui]) == '-')
@@ -370,7 +370,7 @@ int main(int argc, char * unsafe argv[])
370370
printf("ERROR: number of input samples not set\n");
371371
exit(1);
372372
}
373-
373+
374374
if (uiInFs == -1)
375375
{
376376
printf("ERROR: input sample rate not set\n");

tests/asrc_test/model/Main.c

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// ===========================================================================
22
// ===========================================================================
3-
//
3+
//
44
// File: Main.c
55
//
66
// Main implementation file for the ASRC demonstration program
@@ -77,7 +77,7 @@ char* pzOutFileName[ASRC_N_IO_CHANNELS]; // Output data file name
7777

7878
unsigned int uiInFs; // Input sampling rate
7979
unsigned int uiOutFs; // Output sampling rate
80-
float fFsRatioDeviation; // Fs ratio deviation
80+
double fFsRatioDeviation; // Fs ratio deviation
8181

8282
unsigned int uiNTotalInSamples; // Total number of input sample pairs to process
8383
unsigned int uiNTotalOutSamples; // Total number of output sample pairs produced
@@ -94,7 +94,7 @@ ASRCState_t sASRCState[ASRC_N_IO_CHANNELS];
9494
int iASRCStack[ASRC_N_IO_CHANNELS][ASRC_STACK_LENGTH_MULT * N_IN_SAMPLES_MAX];
9595
ASRCCtrl_t sASRCCtrl[ASRC_N_IO_CHANNELS];
9696
int iASRCADFIRCoefs[ASRC_ADFIR_COEFS_LENGTH];
97-
97+
9898

9999
// Data I/O
100100
// --------
@@ -133,13 +133,13 @@ void main(int argc, char *argv[])
133133
int iEndOfFile;
134134
unsigned int ui, uj;
135135

136-
// Display application information
136+
// Display application information
137137
fprintf(stdout, "\n%s \nVersion: %s\n%s\n\n",
138138
APPLICATION, VERSION, COPYRIGHT);
139-
139+
140140
// Initialize default values
141-
pzInFileName[0] = INPUT_FILE_CHANNEL_0_DEFAULT; // Input data for channel 0
142-
pzInFileName[1] = INPUT_FILE_CHANNEL_1_DEFAULT; // Input data for channel 1
141+
pzInFileName[0] = INPUT_FILE_CHANNEL_0_DEFAULT; // Input data for channel 0
142+
pzInFileName[1] = INPUT_FILE_CHANNEL_1_DEFAULT; // Input data for channel 1
143143
pzOutFileName[0] = OUTPUT_FILE_CHANNEL_0_DEFAULT; // Output data for channel 0
144144
pzOutFileName[1] = OUTPUT_FILE_CHANNEL_0_DEFAULT; // Output data for channel 1
145145

@@ -155,7 +155,7 @@ void main(int argc, char *argv[])
155155
uiASRCRandSeed[0] = ASRC_RAND_SEED_CHANNEL_0_DEFAULT; // Random seed for channel 0
156156
uiASRCRandSeed[1] = ASRC_RAND_SEED_CHANNEL_1_DEFAULT; // Random seed for channel 1
157157

158-
// Parse command line arguments
158+
// Parse command line arguments
159159
for (ui = 1; ui < (unsigned int)argc; ui++)
160160
{
161161
if (*(argv[ui]) == '-')
@@ -187,15 +187,15 @@ void main(int argc, char *argv[])
187187

188188
printf("Total number of input samples: %i\n", uiNTotalInSamples);
189189
printf("Number of input samples to process in one call: %i\n\n", uiNInSamples);
190-
190+
191191

192192
// Clear data buffers
193193
for(ui = 0; ui < ASRC_N_IO_CHANNELS * N_TOTAL_IN_SAMPLES_MAX; ui++)
194194
iIn[ui] = 0;
195195
for(ui = 0; ui < ASRC_N_IO_CHANNELS * N_TOTAL_IN_SAMPLES_MAX * ASRC_N_IN_OUT_RATIO_MAX; ui++)
196196
iOut[ui] = 0;
197197

198-
// Open i/o files
198+
// Open i/o files
199199
for(ui = 0; ui < ASRC_N_IO_CHANNELS; ui++)
200200
{
201201
if ((InFileDat[ui] = fopen(pzInFileName[ui], "rt")) == NULL)
@@ -209,7 +209,7 @@ void main(int argc, char *argv[])
209209
HandleError(pzError, FATAL);
210210
}
211211
}
212-
212+
213213

214214
// Process init
215215
// ------------
@@ -280,7 +280,7 @@ void main(int argc, char *argv[])
280280

281281
// Process data
282282
// ------------
283-
// Initialize remaing number of samples to total number of input samples,
283+
// Initialize remaing number of samples to total number of input samples,
284284
// total number of output samples to 0 and setup input / output data pointers to base of buffers
285285
iNRemainingSamples = (int)uiNTotalInSamples;
286286
uiNTotalOutSamples = 0;
@@ -322,7 +322,7 @@ void main(int argc, char *argv[])
322322
// ==================================
323323
for(ui = 0; ui < ASRC_N_IO_CHANNELS; ui++)
324324
// Note: this is block based similar to SSRC, output will be on stack
325-
// and there will be sASRCCtrl[0].uiNSyncSamples samples per channel produced
325+
// and there will be sASRCCtrl[0].uiNSyncSamples samples per channel produced
326326
if(ASRC_proc_F1_F2(&sASRCCtrl[ui]) != ASRC_NO_ERROR)
327327
{
328328
sprintf(pzError, "Error at ASRC F1 F2 process");
@@ -377,13 +377,13 @@ void main(int argc, char *argv[])
377377
// We are back to block based processing. This is where the number of ASRC output samples is required again
378378
// (would not be used if sample by sample based (on output samples))
379379
for(ui = 0; ui < ASRC_N_IO_CHANNELS; ui++)
380-
// Note: this is block based similar to SSRC
380+
// Note: this is block based similar to SSRC
381381
if(ASRC_proc_dither(&sASRCCtrl[ui]) != ASRC_NO_ERROR)
382382
{
383383
sprintf(pzError, "Error at ASRC F1 F2 process");
384384
HandleError(pzError, FATAL);
385385
}
386-
386+
387387

388388
// Write output data to files
389389
// --------------------------
@@ -393,8 +393,8 @@ void main(int argc, char *argv[])
393393
{
394394
sprintf(pzError, "Error while writing to output file, %s", pzOutFileName[ui]);
395395
HandleError(pzError, FATAL);
396-
}
397-
396+
}
397+
398398

399399
// Update input and outut data pointers for next round
400400
piIn += (sASRCCtrl[0].uiNInSamples * ASRC_N_IO_CHANNELS);
@@ -404,21 +404,21 @@ void main(int argc, char *argv[])
404404
// Update total output sample counter
405405
uiNTotalOutSamples += sASRCCtrl[0].uiNASRCOutSamples;
406406
}
407-
408407

409-
// Report MIPS
408+
409+
// Report MIPS
410410
for(ui = 0; ui < ASRC_N_IO_CHANNELS; ui++)
411411
{
412412
printf("MIPS total load channel %i: %f\n", ui, (sASRCCtrl[ui].fCycleCountF1F2 + sASRCCtrl[ui].fCycleCountF3AdaptiveCoefs + sASRCCtrl[ui].fCycleCountF3 + sASRCCtrl[ui].fCycleCountDither) / (float)(sASRCCtrl[ui].uiNInSamples) * (float)uiFsTable[sASRCCtrl[ui].eInFs]/ 1000000.0);
413413
printf("MIPS F1+F2 load channel %i: %f\n", ui, sASRCCtrl[ui].fCycleCountF1F2 / (float)(sASRCCtrl[ui].uiNInSamples) * (float)uiFsTable[sASRCCtrl[ui].eInFs]/ 1000000.0);
414414
printf("MIPS F3 Adaptive Coefs computation load channel %i: %f\n", ui, sASRCCtrl[ui].fCycleCountF3AdaptiveCoefs / (float)(sASRCCtrl[ui].uiNInSamples) * (float)uiFsTable[sASRCCtrl[ui].eInFs]/ 1000000.0);
415415
printf("MIPS F3 load channel %i: %f\n", ui, sASRCCtrl[ui].fCycleCountF3 / (float)(sASRCCtrl[ui].uiNInSamples) * (float)uiFsTable[sASRCCtrl[ui].eInFs]/ 1000000.0);
416416
printf("MIPS Dither load channel %i: %f\n\n", ui, sASRCCtrl[ui].fCycleCountDither / (float)(sASRCCtrl[ui].uiNInSamples) * (float)uiFsTable[sASRCCtrl[ui].eInFs]/ 1000000.0);
417-
417+
418418
}
419419
// Report number of output samples produced
420420
printf("Total number of output samples produced: %i\n\n\n", uiNTotalOutSamples);
421-
421+
422422

423423
// Close i/o files
424424
for(ui = 0; ui < ASRC_N_IO_CHANNELS; ui++)
@@ -435,11 +435,11 @@ void main(int argc, char *argv[])
435435
}
436436
}
437437

438-
438+
439439

440440
fprintf(stdout, "Application run finished, press any key to exit\n\n");
441441
getchar();
442-
442+
443443
exit(0);
444444
}
445445

@@ -461,15 +461,15 @@ void ParseCmdLine(char *input)
461461

462462
case 'e':
463463
case 'E':
464-
fFsRatioDeviation = (float)(atof(input + 1));
464+
fFsRatioDeviation = (double)(atof(input + 1));
465465
break;
466-
466+
467467
case 'h':
468468
case 'H':
469469
ShowUsage();
470470
exit(0);
471471
break;
472-
472+
473473
case 'i':
474474
case 'I':
475475
pzInFileName[0] = input + 1;
@@ -553,7 +553,7 @@ void ShowHelp()
553553
fprintf(stdout,"Invalid argument. Application will terminate now. Use -h to get usage.\n");
554554
fprintf(stdout, "Press any key to exit\n\n");
555555
getchar();
556-
556+
557557
exit(0);
558558
}
559559

@@ -577,7 +577,7 @@ void ShowUsage()
577577
" -r Random seed for channel 0 for dither generation (default: 1)\n\n"
578578
" -s Random seed for channel 1 for dither generation (default: 1458976)\n\n"
579579
);
580-
580+
581581
exit(0);
582582
}
583583

@@ -603,5 +603,3 @@ void HandleError(char *pzErrorMessage, short sErrorType)
603603
getchar();
604604
return;
605605
}
606-
607-

0 commit comments

Comments
 (0)