feature_extrac.m
资源名称:speech.rar [点击查看]
上传用户:ay_070428
上传日期:2014-12-04
资源大小:11427k
文件大小:5k
源码类别:
语音合成与识别
开发平台:
Matlab
- function [parameter,region_set]= feature_extrac(wavefile)
- %feature_extrac(wavefile) :
- %To get feature parameters from input wave file.
- %Generally ,sample rate of the wave file is 8KHz or 16KHz.
- %Inwav is data(time-amplitude) of the wave file.
- %Fs is sample rate(Freqency) of the wave file.
- %NBits means N bits to save one data.
- %[Inwav,Fs,NBits] = wavread('wavedir/w10.wav');
- [Inwav,Fs,NBits] = wavread(wavefile);
- %First Step : pre-emphasis.
- Inwav = pre_emphasis(Inwav);
- %Second Step: frame blocking.
- % Frame size N(ms),overlapping region is M(ms)
- % Generally , M = (1/3)*N , which N = 30.
- N = 32; %32 = (256/8000)*1000 , each frame has 256 points.
- M = (1/2)*N;
- FrameSize = round(N*Fs/1000);
- FrameLap = round(M*Fs/1000);
- FrameCount= floor((size(Inwav,1)/(FrameSize-FrameLap)) - 1);
- parameter = [];
- threshold = 0.0003; % for edge detetcion threshold.
- load tri_coef.mat % for tri_band_filter parameter : fstart,fcent,fstop.
- for i = 1 : FrameCount,
- startpos = (i-1)*(FrameSize-FrameLap)+1;
- Frame{i} = Inwav(startpos:startpos+FrameSize-1);
- %Calculate energy to do edge detection
- energy(i) = sum(Frame{i}.^2)/FrameSize;
- if energy(i) > threshold ,
- energy(i) = 1;
- else
- energy(i) = 0;
- end;
- end;
- [region_set,legal_frame] = edge_detect(energy);
- for i = 1 : FrameCount,
- if ismember(i,legal_frame),
- %Third Step: hamming window.
- % w(m) = 0.54 - 0.46 cos(2*pi*m / n) , 1<=m<=n+1.
- %Wframe = Frame*(0.54 - 0.46*cos(2*pi*i / (FrameCount-1)));
- Wframe = hamming(FrameSize).*Frame{i};
- %Forth Step: fast fourier transform.
- % Using FFT function to calculate.
- % Compute square of real part and imaginary part.
- %fft_frame = sqrt(real(fft(Wframe)).^2+imag(fft(Wframe)).^2);
- fft_frame = abs(fft(Wframe));
- %Fifth Step: triangular bandpass filter.
- % Using user defined function tri_band_filter(fft_frame{i}).
- P = 20; %P means counts of log spectral magnitude.
- TBF_coef = tri_band_filter(fft_frame,P,fstart,fcent,fstop);
- %TBF_coef = bandfilt(fft_frame,P,fstart,fcent,fstop);
- %Sixth Step: cosine transform.
- % Using DCT to get L order mel-scale-cepstrum parameters.
- L = 12; %L means L order , generally L is 12.
- Cepstrum = mel_cepstrum(L,P,TBF_coef);
- %Compute log_energy for parameter.
- normalize_coff = 10;
- log_energy = 10*log10(sum(Frame{i}.^2)/FrameSize)/normalize_coff;
- %parameter is 12+1 = 13 order now.
- %In other words, parameter is 13 x N matrix. N is size of legal_frame.
- temp = [Cepstrum log_energy]';
- parameter = [parameter temp];
- end;
- end;
- %=========compute delta energy and delta cepstrum============
- %Calculate delta Cepstrum and delta log energy
- %Combine them with Cepstrum and log energy, get 26 order parameter.
- delta_window = 2;
- parameter = delta_function(delta_window,parameter);
- %figure;
- %plot(Inwav);
- %========================Subfunction================================
- function Inwav = pre_emphasis(Inwav)
- %s'(n) = s(n)-0.95s(n-1);
- Inwav = Inwav - 0.95*[0 ;Inwav(1:end-1)];
- function TBF_coef = tri_band_filter(fft_frame,P,fstart,fcent,fstop)
- %The function is triangular bandpass filter.
- for i = 1 : P,
- for j = fstart(i) : fcent(i),
- filtmag(j) = (j-fstart(i))/(fcent(i)-fstart(i));
- end;
- for j = fcent(i)+1: fstop(i),
- filtmag(j) = 1-(j-fcent(i))/(fstop(i)-fcent(i));
- end;
- TBF_coef(i) = sum(fft_frame(fstart(i):fstop(i)).*filtmag(fstart(i):fstop(i))');
- end;
- TBF_coef = log(TBF_coef.^2);
- function Cepstrum = mel_cepstrum(L,P,TBF_coef)
- %compute mel-scale cepstrum , L should be 12 at most part.
- for i = 1 : L,
- coef = cos((pi/P)*i*(linspace(1,P,P)-0.5))';
- Cepstrum(i) = sum(coef.*TBF_coef');
- end;
- function parameter = delta_function(delta_window,parameter)
- %compute delta value : delta cepstrum and delta log energy.
- rows = size(parameter,1);
- cols = size(parameter,2);
- temp = [zeros(rows,delta_window) parameter zeros(rows,delta_window)];
- temp2 = zeros(rows,cols);
- denominator = sum([1:delta_window].^2)*2;
- for i = 1+delta_window : cols+delta_window,
- subtrahend = 0;
- minuend = 0;
- for j = 1 : delta_window,
- subtrahend = subtrahend + temp(:,i+j)*j;
- minuend = minuend + temp(:,i-j)*(-j);
- end;
- temp2(:,i-delta_window) = (subtrahend - minuend)/denominator;
- end;
- parameter = [parameter ; temp2];