#include <stdio.h> #include <stdlib.h> #include "wavIo.h" #include "PreAccent.h" #include "HammingWindow.h" #include "Enframe.h" #define factor 0.97 void main() { int len=0;; char* rawfile = "MF.wav"; len=Getlength(rawfile); double *sample; sample=new double[len]; Read(rawfile,sample); /*Pre-cent*/ PreAccent(sample,len,factor );//欲加重,factor 为预加重因子 /*Enframe*/ Enframe(rawfile,sample); /*Hamming windows*/ //Hamming( frames[][512],framenum, winsize); /*FFT of each frame*/ }
#ifndef ML_WAVFILE #define ML_WAVFILE //#include <afx.h> //#include "RealVec.h" #include <stdio.h> bool Read(char *filename,double *s); int Getlength(char *filename); #endif
#include "WavIo.h" #include "stdio.h" #ifndef MAXWORD #define MAXWORD 0xFFFF #endif typedef unsigned int uint; typedef unsigned long dword; typedef unsigned short word; bool Read(char *filename,double *s) { FILE *f = fopen(filename, "rb"); if((f=fopen(filename,"rb"))==NULL) printf("open file failed! "); int size, format, samp_freq, num_samp_bits, NumSamples; dword dw; word w; fread(&dw, sizeof(dword), 1, f); if (dw != 0x46464952) /* RIFF*/ return false; //else //printf("the RIFF is %c; ",dw); fread(&dw, sizeof(dword), 1, f); fread( &dw, sizeof(dword), 1, f); if (dw != 0x45564157) /* WAVE */ return false; //else //printf("the WAVE is %c; ",dw); fread(&dw, sizeof(dword), 1, f); if (dw != 0x20746d66) /* fmt */ return false; fread( &dw, sizeof(dword), 1, f); /* format */ fread(&w, sizeof(word), 1, f); if (w != 7 && w != 1) return false; format=w; /* read the number of channels */ fread( &w, sizeof(word), 1, f); if (w != 1) return false; else printf("the channel is:%d ",w); fread( &dw, sizeof(dword), 1, f); samp_freq=dw; printf("the samp_freq is:%d ",dw); /* Skip a few bytes */ fread( &dw, sizeof(dword), 1, f); fread( &w, sizeof(word), 1, f); fread( &w, sizeof(word), 1, f); if (w!=16) return false; num_samp_bits=w; printf("the samp_bits is:%d ",w); if ((num_samp_bits == 8 && (format != 7 && format != 6)) || (num_samp_bits == 16 && format != 1)) return false; fread( &dw, sizeof(dword), 1, f); if (dw != 0x61746164) /* data */ return false; fread( &dw, sizeof(dword), 1, f); size=dw; if (size == -1) NumSamples = -1; else { if (format == 6 || format == 7) NumSamples = size; else if (format == 1) NumSamples = size / 2; printf("the NumSamples is:%d ",NumSamples); } signed short *buf; int numread; int i=0; //s.Resize(NumSamples); buf = new signed short [NumSamples]; numread = fread( buf, sizeof(signed short), NumSamples, f ); if (numread) { for (int i=0 ; i<NumSamples ; ++i) { s[i] = ( (double) buf[i] ) / MAXWORD; } } delete buf; fclose(f); return true; } int Getlength(char *filename) { FILE *f = fopen(filename, "rb"); if((f=fopen(filename,"rb"))==NULL) printf("open file failed! "); int size, format, samp_freq, num_samp_bits, NumSamples; dword dw; word w; fread(&dw, sizeof(dword), 1, f); if (dw != 0x46464952) /* RIFF*/ return false; fread(&dw, sizeof(dword), 1, f); fread( &dw, sizeof(dword), 1, f); if (dw != 0x45564157) /* WAVE */ return false; fread(&dw, sizeof(dword), 1, f); if (dw != 0x20746d66) /* fmt */ return false; fread( &dw, sizeof(dword), 1, f); /* format */ fread(&w, sizeof(word), 1, f); if (w != 7 && w != 1) return false; format=w; /* read the number of channels */ fread( &w, sizeof(word), 1, f); if (w != 1) return false; fread( &dw, sizeof(dword), 1, f); samp_freq=dw; /* Skip a few bytes */ fread( &dw, sizeof(dword), 1, f); fread( &w, sizeof(word), 1, f); fread( &w, sizeof(word), 1, f); if (w!=16) return false; num_samp_bits=w; if ((num_samp_bits == 8 && (format != 7 && format != 6)) || (num_samp_bits == 16 && format != 1)) return false; fread( &dw, sizeof(dword), 1, f); if (dw != 0x61746164) /* data */ return false; fread( &dw, sizeof(dword), 1, f); size=dw; if (size == -1) NumSamples = -1; else { if (format == 6 || format == 7) NumSamples = size; else if (format == 1) NumSamples = size / 2; fclose(f); return NumSamples; } }
#ifndef ML_Precent #define ML_Precent bool PreAccent(double *sample,int len,double factor); #endif
#include <stdio.h> #include "PreAccent.h" bool PreAccent(double *sample,int len,double factor) { //欲加重公式s2(n) = s(n) - a*s(n-1) for(int i=1;i<len;i++) { sample[i]=sample[i]-factor*sample[i-1]; } return true; }
#ifndef ML_Enframe #define ML_Enframe int Enframe(char *filename,double *sample); #endif
#include <stdio.h> #include "wavIo.h" int Enframe(char* filename ,double *sample) { int NumSamples=Getlength(filename); int i=0,j=0; double frames[600][512]; int winsize=512; int hopsize=128; int framenum=(int)((NumSamples-winsize)/hopsize)+1; //frames = new double [framenum][512]; for(int i=0; i<framenum; i++) { for(int j=0; j<winsize; j++) { frames[i][j] = sample[i*hopsize+j]; } } for(int i=0;i<100;i++) printf("the frames[][] is %f ",frames[i][j]); //delete[] frames; return framenum; }