www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/UpsampleAnAudioSignalUsingFarrowSampleRateConverterExample.m

    %% Upsample an Audio Signal Using Farrow Sample Rate Converter
%%
% *Note*: This example runs only in R2016b or later. If you are using an
% earlier release, replace each call to the function with the equivalent
% |step| syntax. For example, myObject(x) becomes step(myObject,x).

%%
% Create a Farrow rate converter object to convert an audio signal from 
% 44.1 kHz to 96 kHz.
fs1 = 44.1e3;
fs2 = 96e3;
frc = dsp.FarrowRateConverter('InputSampleRate',fs1,...
                              'OutputSampleRate',fs2);
ar = dsp.AudioFileReader('guitar10min.ogg','SamplesPerFrame',14700);
aw = dsp.AudioFileWriter('guitar10min_96kHz.wav','SampleRate',fs2);
%%
% Check the resulting interpolation (L) and decimation (M) factors.
[L,M] = getRateChangeFactors(frc) 
%%
% Display the polynomial the object will use to fit the input samples.
coeffs = getPolynomialCoefficients(frc)
%%
% Convert 100 frames of the audio signal. Write the result to a file.
for n = 1:1:100
   x = ar();  
   y = frc(x);
   aw(y);
end
%%
% Release the |AudioFileWriter| object to complete creation of the output file. 
release(aw)
release(ar)
%%
% Plot the input and output signals of the 100th frame of data. Delay the
% input to compensate for the latency introduced by the filter. 
t1 = 0:1/fs1:1/30-1/fs1;
t2 = 0:1/fs2:1/30-1/fs2;

delay = 4;
el1 = 1:length(t1)-delay;
el2 = 1:length(t2);
el2(1:delay) = [];

figure

subplot(2,1,1)
plot(t1(1:length(el1)),x(el1,1))
hold on
plot(t1(1:length(el1)),x(el1,2))
title('Input channels')

subplot(2,1,2)
plot(t2(1:length(el2)),y(el2,1))
hold on
plot(t2(1:length(el2)),y(el2,2))
xlabel('Time (s)')
title('Output channels')
%%
% Zoom in to see the difference in sample rates.
figure

subplot(2,1,1)
plot(t1(1:length(el1)),x(el1,1),'o-')
hold on
plot(t2(1:length(el2)),y(el2,1),'d--')
xlim([0.0104 0.0107])
title('First channel')
legend('Input','Output')

subplot(2,1,2)
plot(t1(1:length(el1)),x(el1,2),'o-')
hold on
plot(t2(1:length(el2)),y(el2,2),'d--')
xlim([0.0104 0.0107])
xlabel('Time (s)')
title('Second channel')
legend('Input','Output')