48 void ApplyWindow(MyType * __restrict X_fft,
const short *frame,
const MyType *v_hanning,
49 const int framesize,
const int xfftsize)
54 #pragma omp parallel for 56 for(i=0; i<framesize; i++) { X_fft[i]=(MyType)frame[i] * Scaling * v_hanning[i]; }
58 memset(&X_fft[framesize], 0,
sizeof(MyType)*(xfftsize - framesize));
73 void ComputeNorms(MyType *norms, MyType *ts_fk,
const MyType *s_fk,
const int nmidi,
74 const int nbases,
const MyType beta)
78 if (beta>=(MyType)0.0 && beta<=(MyType)0.0)
79 for(i=0; i<nbases; i++) { norms[i]=(MyType)nmidi; }
80 else if (beta>=(MyType)1.0 && beta<=(MyType)1.0)
83 #pragma omp parallel for 85 for(i=0; i<nbases; i++)
97 norms[i]=cblas_sasum(nmidi, &s_fk[k], 1);
99 norms[i]=cblas_dasum(nmidi, &s_fk[k], 1);
103 for(j=0; j<nmidi; j++)
112 for(i=0; i<nbases; i++)
114 norms[i]=cblas_sdot(nmidi, &ts_fk[i*N_MIDI_PAD], 1, &s_fk[i*N_MIDI_PAD], 1);
116 norms[i]=cblas_ddot(nmidi, &ts_fk[i*N_MIDI_PAD], 1, &s_fk[i*N_MIDI_PAD], 1);
120 #pragma omp parallel for 122 for(i=0; i<nbases; i++)
131 for(j=0; j<nmidi; j++)
132 data += ts_fk[k+j]*s_fk[k+j];
152 int AllocAuxiCPU(MyType **norms,
short **frame, MyType **v_cfreq, MyType **v_dxState,
const int nbases,
153 const int tamframe,
const int nmidi)
155 CHECKNULL((*norms) =(MyType *)calloc(nbases,
sizeof(MyType)));
156 CHECKNULL((*v_dxState)=(MyType *)calloc(nbases,
sizeof(MyType)));
157 CHECKNULL((*frame) =(
short *) calloc(tamframe,
sizeof(
short)));
158 CHECKNULL((*v_cfreq) =(MyType *)calloc(nmidi,
sizeof(MyType)));
177 int AllocFFTCPU(MyFFTCPUType *plan, MyType **X_fft, MyType **Out_fft, MyType **Mod_fft,
int *kmin_fft,
178 int *kmax_fft,
const int nfft,
DTWfiles NameFiles)
181 CHECKNULL((*X_fft) =(MyType *)calloc(2*nfft+1,
sizeof(MyType)));
182 CHECKNULL((*Mod_fft)=(MyType *)calloc(nfft,
sizeof(MyType)));
185 CHECKNULL((*Out_fft)=(MyType *)fftwf_malloc(
sizeof(MyType)*nfft));
188 fftwf_init_threads();
190 fftwf_plan_with_nthreads(omp_get_max_threads());
192 fftwf_plan_with_nthreads(sysconf(_SC_NPROCESSORS_CONF));
196 CHECKNULL((*plan)=fftwf_plan_r2r_1d(nfft, (*X_fft), (*Out_fft), FFTW_R2HC, FFTW_MEASURE));
198 CHECKNULL((*Out_fft)=(MyType *)fftw_malloc(
sizeof(MyType)*nfft));
203 fftw_plan_with_nthreads(omp_get_max_threads());
205 fftw_plan_with_nthreads(sysconf(_SC_NPROCESSORS_CONF));
209 CHECKNULL((*plan)= fftw_plan_r2r_1d(nfft, (*X_fft), (*Out_fft), FFTW_R2HC, FFTW_MEASURE));
231 int AllocS_fkCPU(MyType **s_fk, MyType **tauxi, MyType **ts_fk,
const MyType BETA,
const int nmidi,
232 const int nbases,
DTWfiles NameFiles)
236 CHECKNULL((*s_fk)=(MyType *)calloc(nmidi*nbases,
sizeof(MyType)));
240 if (!(BETA>=(MyType)0.0 && BETA<=(MyType)0.0) && !(BETA>=(MyType)1.0 && BETA<=(MyType)1.0))
242 CHECKNULL((*tauxi)=(MyType *)calloc(nmidi,
sizeof(MyType)));
243 CHECKNULL((*ts_fk)=(MyType *)calloc(nmidi*nbases,
sizeof(MyType)));
246 #pragma omp parallel for 248 for (i=0; i<nmidi*nbases; i++)
250 (*ts_fk)[i]=powf((*s_fk)[i], BETA-1.0f);
252 (*ts_fk)[i]= pow((*s_fk)[i], BETA-1.0);
270 int AllocDTWCPU(MyType **pV, MyType **v_SxD,
const int DTWSize,
const int DTWSizePlusPad,
const int startin)
274 CHECKNULL((*pV) =(MyType *)malloc(
sizeof(MyType)*DTWSizePlusPad));
275 CHECKNULL((*v_SxD)=(MyType *)calloc(DTWSize,
sizeof(MyType)));
277 for (i=0; i<DTWSizePlusPad; i++)
285 i = startin % TBLOCK;
287 (*pV)[DTWSizePlusPad-DTWSize] = 0.0;
289 (*pV)[i*(DTWSize + N_COSTS) - DTWSize] = 0.0;
310 int AllocDataCPU(MyType **v_hanning,
int **states_time_i,
int **states_time_e,
int **states_seq,
int **states_corr,
311 int **I_SxD,
int *DTWSize,
const int tamframe,
const int nstates,
DTWfiles NameFiles)
316 CHECKNULL((*v_hanning) =(MyType *)calloc(tamframe,
sizeof(MyType)));
317 CHECKNULL((*states_time_i)= (
int *)calloc(nstates,
sizeof(
int)));
318 CHECKNULL((*states_time_e)= (
int *)calloc(nstates,
sizeof(
int)));
319 CHECKNULL((*states_seq) = (
int *)calloc(nstates,
sizeof(
int)));
320 CHECKNULL((*states_corr) = (
int *)calloc(nstates,
sizeof(
int)));
328 (*DTWSize) = (*states_time_e)[nstates - 1] + 1;
330 CHECKNULL((*I_SxD)=(
int *)calloc((*DTWSize),
sizeof(
int)));
333 for (i=0; i<nstates; i++)
334 for (j=(*states_time_i)[i]; j<=(*states_time_e)[i]; j++)
336 (*I_SxD)[pos]=(*states_seq)[i];
356 void FFT(MyType *v_cfreq,
const int *kmin,
const int *kmax, MyType *X_fft, MyType *Mod_fft,
357 MyType *Out_fft, MyFFTCPUType plan,
const int nfft,
const int nmidi)
367 Mod_fft[0] = Out_fft[0] * Out_fft[0];
368 Mod_fft[nfft/2]= Out_fft[nfft/2]* Out_fft[nfft/2];
371 #pragma omp parallel for 373 for(i=1; i<nfft/2; i++)
374 Mod_fft[i]=Out_fft[i]*Out_fft[i] + Out_fft[nfft-i]*Out_fft[nfft-i];
379 v_cfreq[i]=sqrtf(cblas_sasum(kmax[i]-kmin[i]+1, &Mod_fft[kmin[i]], 1));
381 v_cfreq[i]= sqrt(cblas_dasum(kmax[i]-kmin[i]+1, &Mod_fft[kmin[i]], 1));
385 #pragma omp parallel for 393 for(j=kmin[i];j<=kmax[i];j++) value+=Mod_fft[j];
396 v_cfreq[i]=sqrtf(value);
398 v_cfreq[i]= sqrt(value);
413 int ParIdamin(
const int N,
const MyType *v)
418 PosMin data ={DBL_MAX, 0};
420 #pragma omp parallel for reduction(MIN: data) 422 if (v[i] < data.val) { data.val=v[i]; data.pos=i; }
427 MyType val_min = v[0];
433 MyType val = FLT_MAX;
435 MyType val = DBL_MAX;
438 #pragma omp for nowait 440 if (v[i]<val) { val=v[i]; pos=i; }
442 #pragma omp critical (ZonaCritica1) 444 if (val < val_min) { val_min=val; pos_min=pos; }
445 else if ((val==val_min) && (pos < pos_min)) pos_min=pos;
468 if (v[i]<val) { pos=i; val=v[i]; }
485 int DTWProc(
const MyType *Sequence,
const MyType *Costs, MyType * __restrict pD,
const int NSeq,
const int Where,
const int NST)
487 int NSTplusNC, Ref_Pos, j;
490 NSTplusNC = N_COSTS + NST;
491 Ref_Pos = ((NSeq + N_COSTS) % TBLOCK) * NSTplusNC + N_COSTS - 1;
494 #pragma omp parallel for 509 for(k=0; k<N_COSTS; k++)
511 d2 = Sequence[j]*Costs[k]+pD[Pos-k];
515 for (k=N_COSTS; k<T_COSTS; k++)
517 Pos=((NSeq + (T_COSTS-k)) % TBLOCK) * NSTplusNC + N_COSTS + j - 1;
519 d2 = Sequence[j]*Costs[k]+pD[Pos];
527 j = ParIdamin(NST, &pD[Where]);
550 void ApplyDist(
const MyType *v_dxState, MyType *v_SxD,
const int *I_SxD,
const int size,
const MyType ALPHA)
554 MyType vnorm=(MyType)0.0;
557 #pragma omp parallel for reduction(+: vnorm) 559 for(i=0; i<size; i++)
561 v_SxD[i] = v_dxState[I_SxD[i]];
562 vnorm += (v_SxD[i]*v_SxD[i]);
565 vnorm = 1.0f / (sqrtf(vnorm) + FLT_EPSILON);
569 #pragma omp parallel for 572 v_SxD[i] = 1.0f - expf(ALPHA*fabsf(v_SxD[i]*vnorm));
574 vnorm = 1.0 / (sqrt(vnorm) + DBL_EPSILON);
578 #pragma omp parallel for 581 v_SxD[i] = 1.0 - exp(ALPHA*fabs(v_SxD[i]*vnorm));
600 void ComputeDist(
const MyType *v_cfreq, MyType * __restrict v_dxStates, MyType *tauxi,
601 const MyType *norms,
const MyType *s_fk,
const MyType *ts_fk,
const MyType BETA,
602 const int nbases,
const int nmidi)
606 if (BETA>=(MyType)0.0 && BETA<=(MyType)0.0)
609 #pragma omp parallel for 611 for (i=0;i<nbases;i++)
614 MyType A_kt, dsum, dtmp;
616 itmp = i * N_MIDI_PAD;
621 for (j=0; j<nmidi; j++)
622 A_kt += v_cfreq[j] / s_fk[itmp+j];
623 A_kt = A_kt / norms[i];
625 for (j=0;j<nmidi;j++)
627 dtmp = v_cfreq[j] / (s_fk[itmp+j] * A_kt);
629 dsum += dtmp - logf(dtmp) - 1.0f;
631 dsum += dtmp - log(dtmp) - 1.0;
634 v_dxStates[i] = dsum;
637 else if (BETA>=(MyType)1.0 && BETA<=(MyType)1.0)
639 MyType A_kt=(MyType)0.0;
646 A_kt=cblas_sasum(nmidi, v_cfreq, 1);
648 A_kt=cblas_dasum(nmidi, v_cfreq, 1);
651 for (i=0; i<nmidi; i++) { A_kt += v_cfreq[i]; }
655 #pragma omp parallel for 657 for (i=0;i<nbases;i++)
660 MyType dsum, dtmp, dtmp2, dtmp3;
662 itmp = i * N_MIDI_PAD;
664 dtmp2 = A_kt / norms[i];
666 for (j=0;j<nmidi;j++)
668 dtmp = s_fk[itmp+j] * dtmp2;
671 dsum += dtmp3*logf(dtmp3/dtmp) + dtmp - dtmp3;
673 dsum += dtmp3* log(dtmp3/dtmp) + dtmp - dtmp3;
676 v_dxStates[i] = dsum;
682 BetaMinusOne=BETA-(MyType)1.0,
683 dConst=(MyType)1.0/(BETA*(BETA-(MyType)1.0));
685 for (i=0; i<nmidi; i++)
687 tauxi[i]=powf(v_cfreq[i], BETA);
689 tauxi[i]= pow(v_cfreq[i], BETA);
693 #pragma omp parallel for 695 for (i=0;i<nbases;i++)
698 MyType A_kt, dsum, dtmp, dtmp2, dtmp3;
700 itmp = i * N_MIDI_PAD;
706 A_kt =cblas_sdot(nmidi, v_cfreq, 1, &ts_fk[itmp], 1) / norms[i];
707 dtmp3=powf(A_kt, BetaMinusOne);
709 A_kt =cblas_ddot(nmidi, v_cfreq, 1, &ts_fk[itmp], 1) / norms[i];
710 dtmp3=pow(A_kt,BetaMinusOne);
713 for (j=0; j<nmidi; j++)
714 A_kt += v_cfreq[j] * ts_fk[itmp+j];
715 A_kt = A_kt / norms[i];
717 dtmp3=powf(A_kt, BetaMinusOne);
719 dtmp3= pow(A_kt, BetaMinusOne);
723 for (j=0;j<nmidi;j++)
725 dtmp = s_fk[itmp+j] * A_kt;
726 dtmp2 = ts_fk[itmp+j] * dtmp3;
727 dsum += (tauxi[j] + BetaMinusOne*dtmp2*dtmp - BETA*v_cfreq[j]*dtmp2)*dConst;
729 v_dxStates[i] = dsum;
744 if (fread(&frame[TAMMUESTRA],
sizeof(
short), TTminusTM, fp) != TTminusTM)
758 memmove(frame, &frame[TAMMUESTRA],
sizeof(
short)*TTminusTM);
759 if (fread(&frame[TTminusTM],
sizeof(
short), TAMMUESTRA, fp) != TAMMUESTRA)
return ErrReadFile;
774 int ReadAlsaCPU1st(
short *frame, snd_pcm_t *DeviceID, FILE *fpdump)
776 if (snd_pcm_readi(DeviceID, &frame[TAMMUESTRA], TTminusTM) != TTminusTM)
return ErrReadDevice;
779 if (fwrite(&frame[TAMMUESTRA],
sizeof(
short), TTminusTM, fpdump) != TTminusTM)
return ErrWriteFile;
794 int ReadAlsaCPU(
short *frame, snd_pcm_t *DeviceID, FILE *fpdump)
796 memmove(frame, &frame[TAMMUESTRA],
sizeof(
short)*TTminusTM);
798 if (snd_pcm_readi(DeviceID, &frame[TTminusTM], TAMMUESTRA) != TAMMUESTRA)
return ErrReadDevice;
801 if (fwrite(&frame[TTminusTM],
sizeof(
short), TAMMUESTRA, fpdump) != TAMMUESTRA)
return ErrWriteFile;
817 void BetaNorm(MyType *v_cfreq,
const int nmidi, MyType BETA)
823 if (BETA>=(MyType)0.0 && BETA<=(MyType)0.0)
827 if (BETA>=(MyType)1.0 && BETA<=(MyType)1.0)
830 #pragma omp parallel for reduction(+: value) 832 for(i=0; i<nmidi; i++)
839 #pragma omp parallel for reduction(+: value) 841 for(i=0; i<nmidi; i++)
842 value += powf(v_cfreq[i], BETA);
843 value = powf(value, 1.0/BETA);
846 #pragma omp parallel for reduction(+: value) 848 for(i=0; i<nmidi; i++)
849 value += pow(v_cfreq[i], BETA);
850 value = pow(value, 1.0/BETA);
855 #pragma omp parallel for 857 for(i=0; i<nmidi; i++)
858 v_cfreq[i] = v_cfreq[i]/ value;
Struct for store the name of input/verificaton files. Each composition needs a file with values for ...
void ApplyDist(const MyType *v_dxState, MyType *v_SxD, const int *I_SxD, const int size, const MyType ALPHA)
ApplyDist applies distortion.
int AllocS_fkCPU(MyType **s_fk, MyType **tauxi, MyType **ts_fk, const MyType BETA, const int nmidi, const int nbases, DTWfiles NameFiles)
AllocS_fkCPU Allocates memory for S_fk vector, read its data from file and initializes other auxiliar...
int ReadWavCPU1st(short *frame, FILE *fp)
ReadWavCPU1st reads first audio (frame) from WAV file when ARM is used.
void BetaNorm(MyType *v_cfreq, const int nmidi, MyType BETA)
BetaNorm normalizes v_cfreq vector to have beta-norm 1.
int DTWProc(const MyType *Sequence, const MyType *Costs, MyType *__restrict pD, const int NSeq, const int Where, const int NST)
DTWProc performs the Online-DTW process for the current frame.
Header file with auxiliar functions using by ReMAS, both CPU and GPU.
int ReadVectorInt64(int *vector, const int size, const char *filename)
ReadVectorInt64 fills a int vector with the int64 info stores in a file.
int SeqIdamin(const int N, const MyType *v)
SeqIdamin returns the pos of the minimum in a vector.
int ReadVector(MyType *vector, const int size, const char *filename)
ReadVector fills a MyType vector with the MyType info stores in a file.
Header file for using ReMAS with CPU, both x86_64 and ARM.
int AllocDTWCPU(MyType **pV, MyType **v_SxD, const int DTWSize, const int DTWSizePlusPad, const int startin)
AllocDTWCPU Allocates memory for DTW vectors and initializes them.
void FFT(MyType *v_cfreq, const int *kmin, const int *kmax, MyType *X_fft, MyType *Mod_fft, MyType *Out_fft, MyFFTCPUType plan, const int nfft, const int nmidi)
FFT computes FFT and updates v_cfreq vector.
int ReadS_fk(MyType *s_fk, const int BASES, const char *filename)
ReadS_fk fills the vector s_fk with the info stores in a file.
int AllocFFTCPU(MyFFTCPUType *plan, MyType **X_fft, MyType **Out_fft, MyType **Mod_fft, int *kmin_fft, int *kmax_fft, const int nfft, DTWfiles NameFiles)
AllocFFTCPU Allocates memory for FFT vector and reads some fft information from files.
void ComputeNorms(MyType *norms, MyType *ts_fk, const MyType *s_fk, const int nmidi, const int nbases, const MyType beta)
ComputeNorms fills norms vector.
int ReadWavCPU(short *frame, FILE *fp)
ReadWavCPU reads current audio (frame) from WAV file when ARM is used.
int AllocDataCPU(MyType **v_hanning, int **states_time_i, int **states_time_e, int **states_seq, int **states_corr, int **I_SxD, int *DTWSize, const int tamframe, const int nstates, DTWfiles NameFiles)
AllocDataCPU creates and initializes some structures reading info from files.
void ApplyWindow(MyType *__restrict X_fft, const short *frame, const MyType *v_hanning, const int framesize, const int xfftsize)
ApplyWindow applies hanning window to the current frame and store the result en vector X_fft...
int AllocAuxiCPU(MyType **norms, short **frame, MyType **v_cfreq, MyType **v_dxState, const int nbases, const int tamframe, const int nmidi)
AllocAuxiCPU Memory reservation for norms, frame, v_cfreq and v_dxState vectors.
void ComputeDist(const MyType *v_cfreq, MyType *__restrict v_dxStates, MyType *tauxi, const MyType *norms, const MyType *s_fk, const MyType *ts_fk, const MyType BETA, const int nbases, const int nmidi)
ComputeDist computes distortion.