Skip to content

Commit b3ab693

Browse files
committed
first commit
1 parent 10e7fba commit b3ab693

File tree

18 files changed

+1815
-0
lines changed

18 files changed

+1815
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

1.21 MB
Loading
319 KB
Loading
139 KB
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifdef __cplusplus
2+
extern "C" {
3+
#endif
4+
5+
void cppsetup();
6+
7+
void cpploop();
8+
9+
#ifdef __cplusplus
10+
}
11+
#endif
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#include "stdlib.h"
2+
#include "math.h"
3+
4+
//#define __EXTERN_W__
5+
6+
typedef struct
7+
{
8+
float real;
9+
float imag;
10+
} Complex;
11+
12+
#ifdef __EXTERN_W__
13+
//========Paste the definitions for W here========
14+
const Complex FFT_W[15] =
15+
{
16+
{0.98078528040323,-0.195090322016128},
17+
{0.923879532511287,-0.38268343236509},
18+
{0.831469612302545,-0.555570233019602},
19+
{0.707106781186547,-0.707106781186547},
20+
{0.555570233019602,-0.831469612302545},
21+
{0.38268343236509,-0.923879532511287},
22+
{0.195090322016128,-0.98078528040323},
23+
{5.55111512312578E-17,-1},
24+
{-0.195090322016128,-0.98078528040323},
25+
{-0.38268343236509,-0.923879532511287},
26+
{-0.555570233019602,-0.831469612302545},
27+
{-0.707106781186547,-0.707106781186547},
28+
{-0.831469612302545,-0.555570233019602},
29+
{-0.923879532511286,-0.38268343236509},
30+
{-0.98078528040323,-0.195090322016128}
31+
};
32+
const Complex *w = FFT_W;
33+
//================================================
34+
#endif
35+
36+
unsigned char fftx(Complex *d,int m)
37+
{
38+
#ifndef __EXTERN_W__
39+
static Complex *w;
40+
static int mw = 0;
41+
float arg, w_real, w_imag, wr_real, wr_imag, wtemp;
42+
#endif
43+
static int n = 1;
44+
Complex temp, tmp1, tmp2;
45+
Complex *di, *dip, *dj, *wp;
46+
int i, j, k, l, le, wi;
47+
#ifndef __EXTERN_W__
48+
if(m != mw)
49+
{
50+
if(mw != 0)
51+
free(w);
52+
mw = m;
53+
if(m == 0)
54+
return 0;
55+
n = 1 << m;
56+
le = n >> 1;
57+
w = (Complex*)calloc(le - 1,sizeof(Complex));
58+
if(!w)
59+
return 0;
60+
arg = 4.0 * atan(1.0) / le;
61+
wr_real = w_real = cos(arg);
62+
wr_imag = w_imag = -sin(arg);
63+
dj = w;
64+
for(j = 1; j < le; j++)
65+
{
66+
dj->real = (float)wr_real;
67+
dj->imag = (float)wr_imag;
68+
dj++;
69+
wtemp = wr_real * w_real - wr_imag * w_imag;
70+
wr_imag = wr_real * w_imag + wr_imag * w_real;
71+
wr_real = wtemp;
72+
}
73+
}
74+
#else
75+
n = 1 << m;
76+
#endif
77+
le = n;
78+
wi = 1;
79+
for(l = 0; l < m; l++)
80+
{
81+
le >>= 1;
82+
for(i = 0; i < n; i += (le << 1))
83+
{
84+
di = d + i;
85+
dip = di + le;
86+
temp.real = (di->real + dip->real);
87+
temp.imag = (di->imag + dip->imag);
88+
dip->real = (di->real - dip->real);
89+
dip->imag = (di->imag - dip->imag);
90+
*di = temp;
91+
}
92+
wp = (Complex*)w + wi - 1;
93+
for(j = 1; j < le; j++)
94+
{
95+
tmp1 = *wp;
96+
for(i = j; i < n; i += (le << 1))
97+
{
98+
di = d + i;
99+
dip = di + le;
100+
temp.real = (di->real + dip->real);
101+
temp.imag = (di->imag + dip->imag);
102+
tmp2.real = (di->real - dip->real);
103+
tmp2.imag = (di->imag - dip->imag);
104+
dip->real = (tmp2.real * tmp1.real - tmp2.imag * tmp1.imag);
105+
dip->imag = (tmp2.real * tmp1.imag + tmp2.imag * tmp1.real);
106+
*di = temp;
107+
}
108+
wp += wi;
109+
}
110+
wi <<= 1;
111+
}
112+
113+
for(i = 0; i < n; i++)
114+
{
115+
j = 0;
116+
for(k = 0; k < m; k++)
117+
j = (j << 1) | ((i >> k) & 1);
118+
if(i < j)
119+
{
120+
di = d + i;
121+
dj = d + j;
122+
temp = *dj;
123+
*dj = *di;
124+
*di = temp;
125+
}
126+
}
127+
return 1;
128+
}
129+
130+
void AmpSpectrum(Complex *pData,int m,float* pDCAmp,float* pDistortion)
131+
{
132+
// Amp = Abs / (1.414213562 * N)
133+
int i;
134+
int N = 1 << (m - 1);
135+
int N2 = 1 << m;
136+
float BasePower; //exactly BasePower*2*N*N;
137+
float SyntPower = 0; //exactly SyntPower*2*N*N;
138+
float ModelSquare;
139+
*pDCAmp = sqrt(pData[0].real * pData[0].real + pData[0].imag * pData[0].imag) / (N << 1);
140+
BasePower = pData[1].real * pData[1].real + pData[1].imag * pData[1].imag;
141+
pData[N2 - 1].real = sqrt(BasePower);
142+
pData[N2 - 1].imag = asin(pData[N2 - 1].imag / pData[N2 - 1].real);
143+
pData[N2 - 1].real /= N;
144+
for(i = 2; i < N; i++)
145+
{
146+
ModelSquare = pData[i].real * pData[i].real + pData[i].imag * pData[i].imag;
147+
pData[N2 - i].real = sqrt(ModelSquare);
148+
pData[N2 - i].imag = asin(pData[N2-i].imag/pData[N2-i].real);
149+
pData[N2 - i].real /= N;
150+
SyntPower += ModelSquare;
151+
}
152+
*pDistortion = sqrt(SyntPower / BasePower);
153+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "stdlib.h"
2+
#include "math.h"
3+
4+
typedef struct
5+
{
6+
float real;
7+
float imag;
8+
} Complex;
9+
10+
unsigned char fftx(Complex *x,int m);
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include "codec.h"
2+
#define AUDIO_BUF_NUM 640 /* !<the length of buffer>*/
3+
4+
int16_t* audio_buf = NULL;
5+
int16_t* rx_buffer1 = NULL;
6+
DEV_BUFFER rx_buffer,tx_buffer;
7+
8+
unsigned long frame_energy(int16_t *buf, uint16_t len)
9+
{
10+
unsigned long sum = 0;
11+
int16_t i, tmp;
12+
13+
for(i=0;i<len;i++)
14+
{
15+
tmp = buf[2*i];
16+
sum += tmp * tmp;
17+
}
18+
19+
return sum;
20+
}
21+
22+
uint16_t zc(int16_t *buf, uint16_t len)//zero cross times.
23+
{
24+
uint16_t cnt=0,i;
25+
int16_t tmp1,tmp2;
26+
27+
for(i=0;i<(len-1);i++)
28+
{
29+
tmp1 = buf[2*i];
30+
tmp2 = buf[2*i+2];
31+
if(((tmp1>0)&&(tmp2<0))||((tmp1<0)&&(tmp2>0)))cnt++;
32+
}
33+
34+
return cnt;
35+
}
36+
37+
volatile uint8_t bufcnt=0,word_state=0;
38+
volatile int16_t state_cnt=0;
39+
40+
static void cs4344_rx_isr(void)
41+
{
42+
uint16_t zc_cnt,i;
43+
unsigned long eng;
44+
45+
zc_cnt = zc((rx_buffer1 + 640*bufcnt), 320);
46+
eng = frame_energy((rx_buffer1 + 640*bufcnt), 320);
47+
if((eng>200000000)&&((zc_cnt>4)&&(zc_cnt<55))&&(state_cnt<7))state_cnt++;//audio valid condition.
48+
else if(state_cnt > 0)state_cnt--;
49+
50+
if(bufcnt<17)bufcnt++;
51+
else bufcnt = 0;
52+
53+
if(state_cnt > 6)
54+
{
55+
rx_buffer.buf = audio_buf + 640*16;
56+
rx_buffer.len = 16*1000*2 - 640*16;
57+
cs4344_rx_isr_restart(&rx_buffer);
58+
for(i=0;i<16;i++)
59+
{
60+
if((bufcnt + i + 2)>17)memcpy(audio_buf + i*640,rx_buffer1 + (bufcnt + i + 2 - 18)*640,1280);
61+
else memcpy(audio_buf + i*640,rx_buffer1 + (bufcnt + i + 2)*640,1280);
62+
}
63+
state_cnt = 0;
64+
word_state = 1;
65+
}
66+
else if(word_state == 1)
67+
{
68+
word_state = 2;
69+
rx_buffer.len = 640;
70+
rx_buffer.buf = rx_buffer1 + 640*bufcnt;
71+
}
72+
else
73+
{
74+
rx_buffer.len = 640;
75+
rx_buffer.buf = rx_buffer1 + 640*bufcnt;
76+
cs4344_rx_isr_restart(&rx_buffer);
77+
}
78+
}
79+
80+
void extract_features(MFCC_STR *mfcc_str, const int16_t *audio_data, int8_t *mfcc_buffer)
81+
{
82+
uint16_t f;
83+
int32_t mfcc_buffer_head = 0;
84+
for (f = 0; f < NUM_FRAMES; f++)
85+
{
86+
mfcc_compute(mfcc_str, audio_data + (f * FRAME_SHIFT * 2),&mfcc_buffer[mfcc_buffer_head]);
87+
mfcc_buffer_head += NUM_MFCC_COEFFS;
88+
}
89+
}
90+
91+
static void cs4344_tx_isr(void)
92+
{}
93+
94+
void CODEC_init(int16_t* ao_buf, int16_t* rx_buf)
95+
{
96+
97+
rx_buffer1 = rx_buf;
98+
audio_buf = ao_buf;
99+
100+
rx_buffer.buf = (int16_t *)rx_buffer1;
101+
rx_buffer.len = AUDIO_BUF_NUM;
102+
rx_buffer.ofs = 0;
103+
104+
tx_buffer.buf = (int16_t *)audio_buf;
105+
tx_buffer.len = 16*1000*2;
106+
tx_buffer.ofs = 0;
107+
108+
cs4344_rx_init(CS4344_SAMPLE_FREQ_16, CS4344_DATA_FORMAT_16, CS4344_MODE_ISR, &rx_buffer, cs4344_rx_isr);
109+
cs4344_tx_init(CS4344_SAMPLE_FREQ_16, CS4344_DATA_FORMAT_16, CS4344_MODE_ISR, &tx_buffer, cs4344_tx_isr);
110+
111+
EMBARC_PRINTF("Please connet the mclk !\r\n");
112+
board_delay_ms(5000, OSP_DELAY_OS_COMPAT_DISABLE);
113+
}
114+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef _CODEC_H_
2+
#define _CODEC_H_
3+
4+
#include "embARC.h"
5+
#include "embARC_debug.h"
6+
#include "wm8978i2s.h"
7+
#include "mfcc.h"
8+
9+
void CODEC_init(int16_t* ao_buf, int16_t* rx_buf);
10+
//void CODEC_init();
11+
12+
void extract_features(MFCC_STR *mfcc_str, const int16_t *audio_data, int8_t *mfcc_buffer);
13+
14+
#define audio_buffer_len 16 * 1000 * 2
15+
#endif

0 commit comments

Comments
 (0)