annotate toolboxes/MIRtoolbox1.3.2/AuditoryToolbox/synlpc.m @ 0:cc4b1211e677 tip

initial commit to HG from Changeset: 646 (e263d8a21543) added further path and more save "camirversion.m"
author Daniel Wolff
date Fri, 19 Aug 2016 13:07:06 +0200
parents
children
rev   line source
Daniel@0 1 function synWave = synlpc(aCoeff,source,sr,G,fr,fs,preemp)
Daniel@0 2 % USAGE: synWave = synlpc(aCoeff,source,sr,G,fr,fs,preemp);
Daniel@0 3 %
Daniel@0 4 % This function synthesizes a (speech) signal based on a LPC (linear-
Daniel@0 5 % predictive coding) model of the signal. The LPC coefficients are a
Daniel@0 6 % short-time measure of the speech signal which describe the signal as the
Daniel@0 7 % output of an all-pole filter. This all-pole filter provides a good
Daniel@0 8 % description of the speech articulators; thus LPC analysis is often used in
Daniel@0 9 % speech recognition and speech coding systems. The LPC analysis is done
Daniel@0 10 % using the proclpc routine. This routine can be used to verify that the
Daniel@0 11 % LPC analysis produces the correct answer, or as a synthesis stage after
Daniel@0 12 % first modifying the LPC model.
Daniel@0 13 %
Daniel@0 14 % The results of LPC analysis are a new representation of the signal
Daniel@0 15 % s(n) = G e(n) - sum from 1 to L a(i)s(n-i)
Daniel@0 16 % where s(n) is the original data. a(i) and e(n) are the outputs of the LPC
Daniel@0 17 % analysis with a(i) representing the LPC model. The e(n) term represents
Daniel@0 18 % either the speech source's excitation, or the residual: the details of the
Daniel@0 19 % signal that are not captured by the LPC coefficients. The G factor is a
Daniel@0 20 % gain term.
Daniel@0 21 %
Daniel@0 22 % LPC synthesis produces a monaural sound vector (synWave) which is
Daniel@0 23 % sampled at a sampling rate of "sr". The following parameters are mandatory
Daniel@0 24 % aCoeff - The LPC analysis results, a(i). One column of L+1 numbers for each
Daniel@0 25 % frame of data. The number of rows of aCoeff determines L.
Daniel@0 26 % source - The LPC residual, e(n). One column of sr*fs samples representing
Daniel@0 27 % the excitation or residual of the LPC filter.
Daniel@0 28 % G - The LPC gain for each frame.
Daniel@0 29 %
Daniel@0 30 % The following parameters are optional and default to the indicated values.
Daniel@0 31 % fr - Frame time increment, in ms. The LPC analysis is done starting every
Daniel@0 32 % fr ms in time. Defaults to 20ms (50 LPC vectors a second)
Daniel@0 33 % fs - Frame size in ms. The LPC analysis is done by windowing the speech
Daniel@0 34 % data with a rectangular window that is fs ms long. Defaults to 30ms
Daniel@0 35 % preemp - This variable is the epsilon in a digital one-zero filter which
Daniel@0 36 % serves to preemphasize the speech signal and compensate for the 6dB
Daniel@0 37 % per octave rolloff in the radiation function. Defaults to .9378.
Daniel@0 38 %
Daniel@0 39 % This code was graciously provided by:
Daniel@0 40 % Delores Etter (University of Colorado, Boulder) and
Daniel@0 41 % Professor Geoffrey Orsak (Southern Methodist University)
Daniel@0 42 % It was first published in
Daniel@0 43 % Orsak, G.C. et al. "Collaborative SP education using the Internet and
Daniel@0 44 % MATLAB" IEEE SIGNAL PROCESSING MAGAZINE Nov. 1995. vol.12, no.6, pp.
Daniel@0 45 % 23-32.
Daniel@0 46 % Modified and debugging plots added by Kate Nguyen and Malcolm Slaney
Daniel@0 47
Daniel@0 48 % (c) 1998 Interval Research Corporation
Daniel@0 49 % A more complete set of routines for LPC analysis can be found at
Daniel@0 50 % http://www.ee.ic.ac.uk/hp/staff/dmb/voicebox/voicebox.html
Daniel@0 51
Daniel@0 52
Daniel@0 53
Daniel@0 54 if (nargin < 5), fr = 20; end;
Daniel@0 55 if (nargin < 6), fs = 30; end;
Daniel@0 56 if (nargin < 7), preemp = .9378; end;
Daniel@0 57
Daniel@0 58 msfs = round(sr*fs/1000);
Daniel@0 59 msfr = round(sr*fr/1000);
Daniel@0 60 msoverlap = msfs - msfr;
Daniel@0 61 ramp = [0:1/(msoverlap-1):1]';
Daniel@0 62 [L1 nframe] = size(aCoeff); % L1 = 1+number of LPC coeffs
Daniel@0 63
Daniel@0 64 [row col] = size(source);
Daniel@0 65 if(row==1 | col==1) % continous stream; must be
Daniel@0 66 % windowed
Daniel@0 67 postFilter = 0; duration = length(source); frameIndex = 1;
Daniel@0 68 for sampleIndex=1:msfr:duration-msfs+1
Daniel@0 69 resid(:,frameIndex) = source(sampleIndex:(sampleIndex+msfs-1))';
Daniel@0 70 frameIndex = frameIndex+1;
Daniel@0 71 end
Daniel@0 72 else
Daniel@0 73 postFilter = 1; resid = source;
Daniel@0 74 end
Daniel@0 75
Daniel@0 76 [row col] = size(resid);
Daniel@0 77 %if ~(col==nframe)
Daniel@0 78 % error('synLPC: numbers of LPC frames and source frames do not match');
Daniel@0 79 if col<nframe
Daniel@0 80 nframe=col;
Daniel@0 81 end
Daniel@0 82
Daniel@0 83 for frameIndex=1:nframe
Daniel@0 84 % Calculate the filter response
Daniel@0 85 % by evaluating the z-transform
Daniel@0 86 % if 1
Daniel@0 87 % gain=0;
Daniel@0 88 % cft=0:(1/255):1;
Daniel@0 89 % for index=1:L1-1
Daniel@0 90 % gain = gain + aCoeff(index,frameIndex)*exp(-i*2*pi*cft).^index;
Daniel@0 91 % end
Daniel@0 92 % gain = abs(1./gain);
Daniel@0 93 % spec(:,frameIndex) = 20*log10(gain(1:128))';
Daniel@0 94 % plot(20*log10(gain));
Daniel@0 95 % title(frameIndex);
Daniel@0 96 % drawnow;
Daniel@0 97 % end
Daniel@0 98
Daniel@0 99 % Calculate the filter response
Daniel@0 100 % from the filter's impulse
Daniel@0 101 % response (to check above).
Daniel@0 102 % if 0
Daniel@0 103 % impulseResponse = filter(1, aCoeff(:,frameIndex), [1 zeros(1,255)]);
Daniel@0 104 % freqResp = 20*log10(abs(fft(impulseResponse)));
Daniel@0 105 % plot(freqResp);
Daniel@0 106 % end
Daniel@0 107
Daniel@0 108
Daniel@0 109 A = aCoeff(:,frameIndex);
Daniel@0 110 residFrame = resid(:,frameIndex)*G(frameIndex);
Daniel@0 111 synFrame = filter(1, A', residFrame); % synthesize speech from LPC
Daniel@0 112 % coeffs
Daniel@0 113 if(frameIndex==1) % add synthesize frames using a
Daniel@0 114 synWave = synFrame(1:msfr); % trapezoidal window
Daniel@0 115 else
Daniel@0 116 synWave = [synWave; overlap+synFrame(1:msoverlap).*ramp; ...
Daniel@0 117 synFrame(msoverlap+1:msfr)];
Daniel@0 118 end
Daniel@0 119 if(frameIndex==nframe)
Daniel@0 120 synWave = [synWave; synFrame(msfr+1:msfs)];
Daniel@0 121 else
Daniel@0 122 overlap = synFrame(msfr+1:msfs).*flipud(ramp);
Daniel@0 123 end
Daniel@0 124 %length(synWave)
Daniel@0 125 end;
Daniel@0 126
Daniel@0 127 if(postFilter)
Daniel@0 128 synWave = filter(1, [1 -preemp], synWave);
Daniel@0 129 end
Daniel@0 130