mel_freq_ceps_coeff.c
上传用户:bossps2lzz
上传日期:2022-07-07
资源大小:522k
文件大小:14k
源码类别:

DSP编程

开发平台:

C/C++

  1. /***************************************************************************** 
  2.  * 
  3.  *
  4.  *
  5.  *
  6.  *
  7.  *
  8.  *
  9.  *
  10.  *
  11.  * mel_freq_ceps_coeff.c
  12.  *
  13.  *
  14.  *
  15.  *
  16.  *
  17.  * 
  18.  * Main Program to compute Mel-Frequency Cepstral Co-efficients.
  19.  *
  20.  * The aim of this project is to Compute Mel-Frequency Cepstral Co-efficients.
  21.  * 
  22.  * First the input analog speech signal is digitized at 8KhZ Sampling
  23.  * Frequency using the on board ADC (Analog to Digital Converter)
  24.  * The Speech sample is stored in an one-dimensional array.
  25.  * Speech signal's are quasi-stationary. It means that the 
  26.  * speech signal over a very short time frame can be considered to be a
  27.  * stationary. The speech signal is split into frames. Each frame consists
  28.  * of 256 Samples of Speech signal and the subsequent frame will start from
  29.  * the 100th sample of the previous frame. Thus each frame will overlap
  30.  * with two other subsequent other frames. This technique is called
  31.  * Framing. Speech sample in one frame is considered to be stationary.
  32.  *
  33.  * After Framing, to prevent the spectral lekage we apply windowing. 
  34.  * Here  Hamming window with 256 co-efficients is used.
  35.  *
  36.  * Third step is to convert the Time domain speech Signal into Frequency
  37.  * Domain using Discrete Fourier Transform. Here Fast Fourier Transform
  38.  * is used.
  39.  *
  40.  * The resultant transformation will result in a signal beeing complex
  41.  * in nature. Speech is a real signal but its Fourier Transform will be 
  42.  * a complex one (Signal having both real and imaginary). 
  43.  *
  44.  * The power of the signal in Frequency domain is calculated by summing
  45.  * the square of Real and Imaginary part of the signal in Frequency Domain.
  46.  * The power signal will be a real one. Since second half of the samples
  47.  * in the frame will be symmetric to the first half (because the speech signal
  48.  * is a real one) we ignore the second half (second 128 samples in each frame)
  49.  *
  50.  * Triangular filters are designed using Mel Frequency Scale. These bank of 
  51.  * filters will approximate our ears. The power signal is then applied to 
  52.  * these bank of filters to determine the frequency content across each filter.
  53.  * In our implementation we choose total number of filters to be 20.
  54.  * These 20 filters are uniformly spaced in Mel Frequency scale between 
  55.  * 0-4KhZ.
  56.  *
  57.  * After computing the Mel-Frequency Spectrum, log of Mel-Frequency Spectrum
  58.  * is computed.
  59.  *
  60.  * Discrete Cosine Tranform of the resulting signal will result in the 
  61.  * computation of the Mel-Frequency Cepstral Co-efficient.
  62.  *
  63.  *
  64.  *
  65.  *
  66.  * Written by Vasanthan Rangan and Sowmya Narayanan
  67.  * 
  68.  * 
  69.  * Version 1.2
  70.  *
  71.  *
  72.  *
  73.  *
  74.  ******************************************************************************/
  75. /*****************************************************************************
  76.  *
  77.  * Include Header Files
  78.  *
  79.  *
  80.  ******************************************************************************/
  81. #include "dsk6713_aic23.h"
  82. Uint32 fs=DSK6713_AIC23_FREQ_8KHZ;
  83. #include <stdio.h>
  84. #include <math.h>
  85. #include "block_dc.h" // Header file for identifying the start of speech signal
  86. #include "detect_envelope.h" // Header file for identfying the start of speech signal
  87. /*****************************************************************************
  88.  * 
  89.  *
  90.  * Definition of Variables
  91.  *
  92.  *
  93.  *****************************************************************************/
  94. #define Number_Of_Filters 20 // Number of Mel-Frequency Filters
  95. #define column_length 256 // Frame Length of the one speech signal
  96. #define row_length 100 // Total number of Frames in the given speech signal
  97. #define PI 3.14159
  98. /*****************************************************************************
  99.  *
  100.  *
  101.  * Custom Structure Definition
  102.  *
  103.  *
  104.  *****************************************************************************/
  105.  
  106.  
  107. struct complex { 
  108. float real;
  109. float imag;
  110. }; // Generic Structure to represent real and imaginary part of a signal
  111. struct buffer {
  112. struct complex data[row_length][column_length];
  113. }; // Structure to store the input speech sample
  114. struct mfcc {
  115. float data[row_length][Number_Of_Filters];
  116. }; // Structure to store the Mel-Frequency Co-efficients
  117. /*****************************************************************************
  118.  *
  119.  *
  120.  * Assigning the data structures to external memory
  121.  *
  122.  *
  123.  *****************************************************************************/
  124.  
  125.  
  126.            
  127. #pragma DATA_SECTION(real_buffer,".EXTRAM")
  128. struct buffer real_buffer; //real_buffer is used to store the input speech.
  129. #pragma DATA_SECTION(coeff,".EXTRAM")
  130. struct mfcc coeff; //coeff is used to store the Mel-Frequency Spectrum.
  131. #pragma DATA_SECTION(mfcc_ct,".EXTRAM")
  132. struct mfcc mfcc_ct; //mfcc_ct is used to store the Mel-Frequency Cepstral Co-efficients.
  133. /*****************************************************************************
  134.  *
  135.  *
  136.  * Variable Declaration
  137.  *
  138.  *
  139.  *****************************************************************************/
  140.  
  141.  
  142. int gain;           /* output gain (Used during Play-Back */
  143. int signal_status; /* Variable to detect speech signal */
  144. int count; /* Variable to count */
  145. int column; /* Variable used for incrementing column (Samples inside Frame)*/
  146. int row; /* Variable used for incrementing row(Number of Frames)*/
  147. int program_control; /* Variable to identify where the program is
  148. Example: program_control=0 means program is 
  149. capturing input speech signal
  150. program_control=1 means that program has finished
  151. capturing input and ready for processing. At this
  152. time the input speech signal is replayed back
  153. program_control=2 means program is ready for 
  154. idenitification. */
  155. FILE *fptr;
  156. /*****************************************************************************
  157.  *
  158.  *
  159.  * Function Declaration
  160.  *
  161.  *
  162.  *****************************************************************************/
  163.  
  164.  
  165. void fft (struct buffer *, int , int ); /* Function to compute Fast Fouruer Transform */
  166. short playback(); /* Function for play back */
  167. void log_energy(struct mfcc *); /* Function to compute Log of Power Signal */
  168. void mfcc_coeff(struct mfcc * , struct mfcc *); /* Function to compute MFCC */
  169. interrupt void c_int11()            /* interrupt service routine */
  170. {
  171. short sample_data;
  172. short out_sample;
  173. if ( program_control == 0 ) { /* Beginning of Capturing input speech */
  174.    
  175. sample_data = input_sample();           /* input data */
  176. signal_status = framing_windowing(sample_data, &real_buffer); /* Signal Identification
  177.    * and Framing and Windowing */
  178. out_sample = 0; /* Output Data */
  179. if (signal_status > 0) {
  180. program_control = 1;        /* Capturing input signal is done */
  181. }
  182. output_sample(out_sample); /* play nothing */
  183. }
  184. if ( program_control == 1 ) { /* Beginning of the Play back */
  185. out_sample = playback(); /* call the playback funciton to get the 
  186.   * stored speech sample */
  187.   
  188. output_sample(out_sample); /* play the output speech sample */
  189. }
  190. return;
  191. }
  192. void main()  { /* Main Function of the program */
  193. /****************************************************************************
  194.  *
  195.  * Declaring Local Variables
  196.  *
  197.  *
  198.  *****************************************************************************/
  199.  
  200. int i; /* Variable used for counters */
  201.    int j; /* Variable used for Counters */
  202.   
  203.    int stages; /* Variable to identify total number of stages */
  204.   
  205.     
  206.  
  207. /*****************************************************************************
  208.  *
  209.  *
  210.  * Execution of functions start
  211.  *
  212.  *
  213.  ******************************************************************************/
  214.    
  215. comm_intr();   /* init DSK, codec, McBSP */
  216.   
  217. /******************************************************************************
  218.  *
  219.  *
  220.  * Initializing Variables
  221.  *
  222.  *
  223.  *****************************************************************************/
  224.   gain = 1;
  225. column = 0;
  226. row = 0;
  227. program_control = 0;
  228. signal_status = 0;
  229. count = 0;   
  230. stages=8; /* Total Number of stages in FFT = 8 */
  231. for ( i=0; i < row_length ; i++ ) { /* Total Number of Frames */
  232.  
  233.  
  234.    for ( j = 0; j < column_length ; j++) { /* Total Number of Samples in a Frame */
  235.   
  236.    real_buffer.data[i][j].real = 0.0; /* Initializing real part to be zero */
  237.   
  238.    real_buffer.data[i][j].imag = 0.0; /* Initializing imaginary part to be zero*/
  239. }
  240.    }
  241.   
  242.    for ( i=0; i<row_length; i++) { /* Total Number of Frames */
  243.   
  244.    for ( j=0; j<Number_Of_Filters; j++) { /* Total Number of Filters */
  245.   
  246. coeff.data[i][j] = 0.0; /* Initializing the co-effecient array */
  247. mfcc_ct.data[i][j] = 0.0; /* Initializing the array for storing MFCC */
  248. }
  249. } /* End of Initializing the variables to zero */
  250. /*****************************************************************************
  251. *
  252. *
  253. * Begining of the execution of the functions.
  254. *
  255. *
  256. *****************************************************************************/
  257. while(program_control == 0);      /* infinite loop For Receiving/capturing alone*/
  258.    while(program_control ==1); /* infinite loop for playback alone*/
  259. /* Compute FFT of the input speech signal after Framing and Windowing */
  260. fft(&real_buffer,column_length,stages);
  261. /* Compute Power Spectrum of the speech signal in Frequency Domain Representation */
  262. power_spectrum(&real_buffer);
  263. /* Compute Mel-Frequency Spectrum of the speech signal in Power Spectrum Form */
  264. mel_freq_spectrum(&real_buffer,&coeff);
  265. /* Computation of Log of the Power Spectrum */
  266. log_energy(&coeff);
  267. /* Computation of Discrete Cosine Transform */
  268. mfcc_coeff(&mfcc_ct,&coeff);
  269. fptr = fopen("mfcc.coeff","w");
  270. for (i=0; i< row_length; i++) {
  271. for (j = 0; j< Number_Of_Filters; j++) {
  272. fprintf(fptr, "%f, ",mfcc_ct.data[i][j]);
  273. }
  274. }
  275. fclose(fptr);
  276. }
  277.  
  278. /* Function to Compute Fast Fourier Transform */
  279.  
  280. void fft (struct buffer *input_data, int n, int m) {/* Input speech Data, n = 2^m, m = total number of stages */
  281. int n1,n2,i,j,k,l,row_index; /* Declare Variables
  282.   * n1 is the difference between upper and lower 
  283.   * i,j,k,l are counters
  284.   * row_index is used to index every frame */
  285.   
  286. float xt,yt,c,s,e,a; /* declare variables for storing temporary values
  287.     * xt,yt for temporary real and Imaginary respectively
  288.     * c for cosine
  289.     * s for sine
  290.     * e and a for computing the input to cosine and sine
  291.     */
  292.     
  293. for ( row_index = 0; row_index < row_length; row_index++) { /* For every frame */
  294. /* Loop through all the stages */
  295. n2 = n;
  296. for ( k=0; k<m; k++) {
  297. n1 = n2;
  298. n2 = n2/2;
  299. e = PI/n1;
  300. /* Compute Twiddle Factors */
  301. for ( j= 0; j<n2; j++) {
  302. a = j*e;
  303. c = (float) cos(a);
  304. s = (float) sin(a);
  305. /* Do the Butterflies for all 256 samples */
  306. for (i=j; i<n; i+= n1) {
  307. l = i+n2;
  308. xt = input_data->data[row_index][i].real - input_data->data[row_index][l].real;
  309. input_data->data[row_index][i].real = input_data->data[row_index][i].real+input_data->data[row_index][l].real;
  310. yt = input_data->data[row_index][i].imag - input_data->data[row_index][l].imag;
  311. input_data->data[row_index][i].imag = input_data->data[row_index][i].imag+input_data->data[row_index][l].imag;
  312. input_data->data[row_index][l].real = c*xt + s*yt;
  313. input_data->data[row_index][l].imag = c*yt - s*yt;
  314. }
  315. }
  316. }
  317. /* Bit Reversal */
  318. j = 0;
  319. for ( i=0; i<n-1; i++) {
  320. if (i<j) {
  321. xt = input_data->data[row_index][j].real;
  322. input_data->data[row_index][j].real = input_data->data[row_index][i].real;
  323. input_data->data[row_index][i].real = xt;
  324. yt = input_data->data[row_index][j].imag;
  325. input_data->data[row_index][j].imag = input_data->data[row_index][i].imag;
  326. input_data->data[row_index][i].imag = yt;
  327. }
  328. }
  329. }
  330. return;
  331. }
  332. /* Function to compute log of Mel-Frequency spectrum */
  333. void log_energy(struct mfcc *co_eff) {
  334. int i,j; /* Variables declared to act as counters */
  335.    for ( i=0; i<row_length; i++) { /* For all the frames (100 Frames)) */
  336.   
  337. for ( j=0; j<Number_Of_Filters; j++ ) { /* For all the filters (20 Filters)*/
  338. co_eff->data[i][j] = (float) log((double) co_eff->data[i][j]); /* Compute log of co-efficients */
  339. }
  340.    }
  341. }
  342. /* Function to compute Discrete Cosine Transform */
  343. void mfcc_coeff(struct mfcc *mfccct, struct mfcc *co_eff) {
  344. int i,j,k; /* Variable declared to act as counters */
  345. for ( i=0; i<row_length; i++) { /* For all the frames (100 Frames) */
  346.    for (j=0; j<Number_Of_Filters; j++ ) { /* For all the filters */
  347.   
  348.    mfccct->data[i][j] = 0.0;
  349.   
  350. /* Compute Cosine Transform of the Signal */
  351.    for ( k=0; k<Number_Of_Filters; k++) {
  352.    mfccct->data[i][j] = mfccct->data[i][j] + co_eff->data[i][k]*cos((double)((PI*j*(k-1/2))/Number_Of_Filters));
  353.    }
  354.    }
  355.    }
  356.   
  357. }
  358. /* Function to play back the speech signal */
  359. short playback() {
  360. column++; /* Variable to store the index of speech sample in a frame */
  361. if ( column >= column_length ) { /* If Colum >=256 reset it to zero
  362.   * and increment the frame number */
  363. column = 0; /* initialize the sample number back to zero */
  364. row++;  /* Increment the Frame Number */
  365. }
  366. if ( row >= row_length ) { /* If Total Frame Number reaches 100 initialize
  367. * row to be zero
  368. * and change the program control inidcating
  369. * end of playback */
  370. program_control = 2; /* End of Playback */
  371. row = 0; /* Initialize the frame number back to zero */
  372. }
  373. return ((int)real_buffer.data[row][column].real); /* Return the stored speech Sample */
  374. }