efficient sample rate conversion between arbitrary factors -凯发k8网页登录
this example shows how to efficiently convert sample rates between arbitrary factors.
the need for sample rate conversion by an arbitrary factor arises in many applications (e.g. symbol synchronization in digital receivers, speech coding and synthesis, computer simulation of continuous-time systems, etc.). in this example, we will examine an example where cascades of polynomial-based and polyphase filters form an efficient solution when it is desired to convert the sampling rate of a signal from 8 khz to 44.1 khz.
single stage polyphase approach
polyphase structures are generally considered efficient implementations of multirate filters. however in the case of fractional sample rate conversion, the number of phases, and therefore the filter order, can quickly become excessively high. to resample a signal from 8 khz to 44.1 khz, we interpolate by 441 and decimate by 80 (8*441/80=44.1).
samprateconv = dsp.samplerateconverter('bandwidth',6e3, ... 'inputsamplerate',8e3,'outputsamplerate',44.1e3, ... 'stopbandattenuation',50);
this can be done in relatively efficient manner in two stages:
info(samprateconv)
ans = 'overall interpolation factor : 441 overall decimation factor : 80 number of filters : 2 multiplications per input sample: 95.175000 number of coefficients : 1774 filters: filter 1: dsp.firrateconverter - interpolation factor: 147 - decimation factor : 80 filter 2: dsp.firinterpolator - interpolation factor: 3 '
cost(samprateconv)
ans = struct with fields:
numcoefficients: 1774
numstates: 30
multiplicationsperinputsample: 95.1750
additionsperinputsample: 89.6750
although the number of operations per input sample is reasonable (roughly 95 multiplications - keeping in mind that the rate increases after the first stage to 14.7 khz), 1774 coefficients would have to be stored in memory in this case.
providing an output rate tolerance
one way to mitigate the large number of coefficients could be to allow for a tolerance in the output sample rate if the exact rate is not critical. for example, specifying a tolerance of 1% results in an output rate of 44 khz rather then 44.1 khz. this now requires to interpolate by 11 and decimate by 2. it can be done efficiently with a single stage.
samprateconvwithtol = dsp.samplerateconverter('bandwidth',6e3, ... 'inputsamplerate',8e3,'outputsamplerate',44.1e3, ... 'stopbandattenuation',50,'outputratetolerance',0.01); cost(samprateconvwithtol)
ans = struct with fields:
numcoefficients: 120
numstates: 12
multiplicationsperinputsample: 60
additionsperinputsample: 55
in this case, 120 coefficients are needed and the number of multiplications per input sample is 60.
single stage farrow approach
polynomial-based filters are another way to overcome the problem of needing a large number of coefficients to be stored. farrow structures are efficient implementations for such filters.
farrowsamprateconv_3rd = dsp.farrowrateconverter('inputsamplerate',8e3, ... 'outputsamplerate',44.1e3,'polynomialorder',3); farrowsamprateconv_4th = dsp.farrowrateconverter('inputsamplerate',8e3, ... 'outputsamplerate',44.1e3,'polynomialorder',4); cost(farrowsamprateconv_3rd)
ans = struct with fields:
numcoefficients: 16
numstates: 3
multiplicationsperinputsample: 66.1500
additionsperinputsample: 60.6375
cost(farrowsamprateconv_4th)
ans = struct with fields:
numcoefficients: 25
numstates: 4
multiplicationsperinputsample: 121.2750
additionsperinputsample: 99.2250
with 3rd-order polynomials, 16 coefficients are needed and about 66 multiplications per input sample. fourth-order polynomials provide slightly better lowpass response at a higher cost: 25 coefficients and 121 multiplications per input sample.
filts = getfilters(samprateconv); w = linspace(0,44.1e3,2048); % define the frequency range analysis fs1 = 8e3*147; % the equivalent single stage filter is clocked at 3.53 mhz hfvt = fvtool(filts.stage1,farrowsamprateconv_3rd, ... farrowsamprateconv_4th,'frequencyrange','specify freq. vector', ... 'frequencyvector',w,'fs',[fs1 3*fs1 3*fs1], ... 'normalizemagnitudeto1','on','color','white'); legend(hfvt,'polyphase sample-rate converter', ... '3rd-order farrow interpolator','4th-order farrow interpolator', ... 'location','northeast')
providing an output rate tolerance does not significantly impact the implementation cost of the farrow filter. however, it does change the interpolation and decimation factors in the same way it does for dsp.samplerateconverter.
farrowsamprateconv_4th = dsp.farrowrateconverter('inputsamplerate',8e3, ... 'outputsamplerate',44.1e3,'polynomialorder',4, ... 'outputratetolerance',0.01); info(farrowsamprateconv_4th)
ans = 12x52 char array
'discrete-time fir multirate filter (real) '
'----------------------------------------- '
'filter structure : farrow sample-rate converter'
'interpolation factor : 11 '
'decimation factor : 2 '
'filter length : 5 '
'stable : yes '
'linear phase : no '
' '
'arithmetic : double '
'output rate tolerance : 1.000000 % '
'adjusted output rate : 44000.000000 '
cost(farrowsamprateconv_4th)
ans = struct with fields:
numcoefficients: 25
numstates: 4
multiplicationsperinputsample: 121
additionsperinputsample: 99
cascade of farrow and fir polyphase structures
we now try to design a hybrid solution that would take advantage of the two types of filters that we have previously seen. polyphase filters are particularly well adapted for interpolation or decimation by an integer factor and for fractional rate conversions when the interpolation and the decimation factors are low. farrow filters can efficiently implement arbitrary (including irrational) rate change factors. first, we interpolate the original 8 khz signal by 4 using a cascade of fir halfband filters.
intsamprateconv = dsp.samplerateconverter('bandwidth',6e3, ... 'inputsamplerate',8e3,'outputsamplerate',32e3, ... 'stopbandattenuation',50); info(intsamprateconv)
ans = 'overall interpolation factor : 4 overall decimation factor : 1 number of filters : 1 multiplications per input sample: 34.000000 number of coefficients : 34 filters: filter 1: dsp.firinterpolator - interpolation factor: 4 '
then, we interpolate the intermediate 32 khz signal by 44.1/32 = 1.378125 to get the desired 44.1 khz final sampling frequency. we use a cubic lagrange polynomial-based filter for this purpose.
farrowsamprateconv = dsp.farrowrateconverter('inputsamplerate',32e3, ... 'outputsamplerate',44.1e3,'polynomialorder',3);
the overall filter is simply obtained by cascading the two filters.
cost(intsamprateconv)
ans = struct with fields:
numcoefficients: 34
numstates: 11
multiplicationsperinputsample: 34
additionsperinputsample: 31
cost(farrowsamprateconv)
ans = struct with fields:
numcoefficients: 16
numstates: 3
multiplicationsperinputsample: 16.5375
additionsperinputsample: 15.1594
the number of coefficients of this hybrid design is relatively low (36) and the number of multiplications per input sample is also relatively low: 28 16*4 = 92. the combined frequency response of these two designs is superior to that of farrowsamprateconv_3rd
or farrowsamprateconv_4th
.
[hsrc,f] = freqz(intsamprateconv); fsfar = 32e3*441; hfsrc = freqz(farrowsamprateconv,f,fsfar); hhybrid = hsrc.*hfsrc; hhybrid_norm = hhybrid/norm(hhybrid,inf); % normalize magnitude to 0 db plot(f,20*log10(abs(hhybrid_norm))); xlabel('frequency (hz)') ylabel('magnitude (db)') legend('combined polyphase and farrow sample rate converters', ... 'location','northeast')
we now overlay the frequency responses of the single-stage and the multistage designs. clearly the responses are very comparable.
scope = spectrumanalyzer('samplerate',44.1e3,'plotastwosidedspectrum',false, ... 'ylimits',[-80 20],'showlegend',true, ... 'channelnames',{'single-stage design','multi-stage design'}); tic, while toc < 20 % run for 20 seconds x = randn(8000,1); % convert rate using multistage fir filters y1 = samprateconv(x); % convert rate using cascade of multistage fir and farrow filter ytemp = intsamprateconv(x); y2 = farrowsamprateconv(ytemp); % compare the output from both approaches scope([y1,y2]) end