2012-05-22 13:09:48 -04:00
|
|
|
#include "getfile.h"
|
|
|
|
#include <QDir>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <math.h>
|
|
|
|
|
2012-09-26 11:20:43 -04:00
|
|
|
void getfile(QString fname, int ntrperiod)
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
|
|
|
|
|
|
|
char name[80];
|
|
|
|
strcpy(name,fname.toAscii());
|
|
|
|
FILE* fp=fopen(name,"rb");
|
|
|
|
|
2012-09-26 11:20:43 -04:00
|
|
|
int npts=ntrperiod*12000;
|
2012-09-25 12:04:38 -04:00
|
|
|
memset(jt8com_.d2,0,2*npts);
|
2012-05-22 13:09:48 -04:00
|
|
|
|
2012-07-05 12:16:03 -04:00
|
|
|
if(fp != NULL) {
|
2012-07-06 16:31:42 -04:00
|
|
|
// Read (and ignore) a 44-byte WAV header; then read data
|
2012-09-25 12:04:38 -04:00
|
|
|
fread(jt8com_.d2,1,44,fp);
|
|
|
|
int nrd=fread(jt8com_.d2,2,npts,fp);
|
2012-05-22 13:09:48 -04:00
|
|
|
fclose(fp);
|
2012-09-26 14:45:10 -04:00
|
|
|
// for(int i=0; i<npts; i++) jt8com_.d2[i]/=100;
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 11:20:43 -04:00
|
|
|
void savewav(QString fname, int ntrperiod)
|
2012-05-22 13:09:48 -04:00
|
|
|
{
|
2012-07-05 15:18:58 -04:00
|
|
|
struct {
|
|
|
|
char ariff[4];
|
|
|
|
int nchunk;
|
|
|
|
char awave[4];
|
|
|
|
char afmt[4];
|
|
|
|
int lenfmt;
|
|
|
|
short int nfmt2;
|
|
|
|
short int nchan2;
|
|
|
|
int nsamrate;
|
|
|
|
int nbytesec;
|
|
|
|
short int nbytesam2;
|
|
|
|
short int nbitsam2;
|
|
|
|
char adata[4];
|
|
|
|
int ndata;
|
|
|
|
} hdr;
|
|
|
|
|
2012-09-26 11:20:43 -04:00
|
|
|
int npts=ntrperiod*12000;
|
2012-07-05 12:16:03 -04:00
|
|
|
// qint16* buf=(qint16*)malloc(2*npts);
|
2012-05-22 13:09:48 -04:00
|
|
|
char name[80];
|
|
|
|
strcpy(name,fname.toAscii());
|
|
|
|
FILE* fp=fopen(name,"wb");
|
|
|
|
|
|
|
|
if(fp != NULL) {
|
2012-07-05 12:16:03 -04:00
|
|
|
// Write a WAV header
|
2012-07-05 15:18:58 -04:00
|
|
|
hdr.ariff[0]='R';
|
|
|
|
hdr.ariff[1]='I';
|
|
|
|
hdr.ariff[2]='F';
|
|
|
|
hdr.ariff[3]='F';
|
|
|
|
hdr.nchunk=0;
|
|
|
|
hdr.awave[0]='W';
|
|
|
|
hdr.awave[0]='A';
|
|
|
|
hdr.awave[0]='V';
|
|
|
|
hdr.awave[0]='E';
|
|
|
|
hdr.afmt[0]='f';
|
|
|
|
hdr.afmt[1]='m';
|
|
|
|
hdr.afmt[2]='t';
|
|
|
|
hdr.afmt[3]=' ';
|
|
|
|
hdr.lenfmt=16;
|
|
|
|
hdr.nfmt2=1;
|
|
|
|
hdr.nchan2=1;
|
2012-09-26 11:20:43 -04:00
|
|
|
hdr.nsamrate=12000;
|
|
|
|
hdr.nbytesec=2*12000;
|
2012-07-05 15:18:58 -04:00
|
|
|
hdr.nbytesam2=2;
|
|
|
|
hdr.nbitsam2=16;
|
|
|
|
hdr.adata[0]='d';
|
|
|
|
hdr.adata[1]='a';
|
|
|
|
hdr.adata[2]='t';
|
|
|
|
hdr.adata[3]='a';
|
|
|
|
hdr.ndata=2*npts;
|
2012-07-05 12:16:03 -04:00
|
|
|
|
2012-07-05 15:18:58 -04:00
|
|
|
fwrite(&hdr,sizeof(hdr),1,fp);
|
2012-09-25 12:04:38 -04:00
|
|
|
// memcpy(jt8com_.d2,buf,2*npts);
|
2012-07-05 12:16:03 -04:00
|
|
|
// fwrite(buf,2,npts,fp);
|
2012-09-25 12:04:38 -04:00
|
|
|
fwrite(jt8com_.d2,2,npts,fp);
|
2012-05-22 13:09:48 -04:00
|
|
|
fclose(fp);
|
|
|
|
}
|
2012-07-05 12:16:03 -04:00
|
|
|
// free(buf);
|
2012-05-22 13:09:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//#define MAX_RANDOM 0x7fffffff
|
|
|
|
|
|
|
|
/* Generate gaussian random float with mean=0 and std_dev=1 */
|
|
|
|
float gran()
|
|
|
|
{
|
|
|
|
float fac,rsq,v1,v2;
|
|
|
|
static float gset;
|
|
|
|
static int iset;
|
|
|
|
|
|
|
|
if(iset){
|
|
|
|
/* Already got one */
|
|
|
|
iset = 0;
|
|
|
|
return gset;
|
|
|
|
}
|
|
|
|
/* Generate two evenly distributed numbers between -1 and +1
|
|
|
|
* that are inside the unit circle
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
v1 = 2.0 * (float)rand() / RAND_MAX - 1;
|
|
|
|
v2 = 2.0 * (float)rand() / RAND_MAX - 1;
|
|
|
|
rsq = v1*v1 + v2*v2;
|
|
|
|
} while(rsq >= 1.0 || rsq == 0.0);
|
|
|
|
fac = sqrt(-2.0*log(rsq)/rsq);
|
|
|
|
gset = v1*fac;
|
|
|
|
iset++;
|
|
|
|
return v2*fac;
|
|
|
|
}
|