Accelerating the pace of engineering and science

# Documentation Center

• Trials

## Audio Sample-Rate Conversion

This example shows how to use multirate filters for sample rate conversion of different audio formats.

Converting the Sampling Rate from 44.1 KHz to 96 KHz

The conversion from an audio signal sampled at 44.1 kHz (CD quality) to an audio signal sampled at 96 kHz (DVD quality) can be done in two steps. Before describing the two steps, an audio file reader is created to provide the input audio signal. In this example an OGG file is used, but it can replaced with any (MP3, FLAC, WAV, M4A) audio file as long as it is sampled at 44.1 kHz.

```% Here you create a System object to read from an audio file and
% determine the file's audio sampling rate.
frameSize = 14700;
'SamplesPerFrame', frameSize, ...
'PlayCount', Inf, ...
'OutputDataType', 'double');

fileInfo = info(hfileIn);
inFs = fileInfo.SampleRate; % Input Fs assumed to be 44.1 kHz
```

First Stage of Conversion

As a first step, a rational sample-rate converter (RSRC) filter can be used to convert from 44.1 kHz to 48 kHz. This requires an interpolation by a factor of 160 followed by a decimation by a factor of 147. A possible design of a filter that achieves this is:

```L    = 160;    % Interpolation factor
M    = 147;    % Decimation factor
TW   = 4100;   % Transition width in Hz
Ast  = 100;    % Stopband attenuation in dB
fsrc = fdesign.rsrc(L,M,'Nyquist',L,'TW,Ast',TW,Ast,inFs*L);
hsrc = design(fsrc,'kaiserwin','SystemObject',true);
```

The transition width was chosen as 4100 Hz because audio is typically assumed to occupy a band of interest between 0 Hz and 20 kHz. The sampling rate of 44.1 kHz allows for a transition band of 44.1e3 - 2*Fmax which corresponds to 4100 Hz.

Second Stage of Conversion

For the next step, a halfband interpolation filter can be used to convert from 48 kHz to 96 kHz. The interpolation factor is 2. A possible design of this filter is:

```midFs = inFs*L/M; % 48 kHz
L2    = 2;        % Interpolation factor
TW    = 8e3;      % Transition width in Hz
Ast   = 100;      % Stopband attenuation in dB
outFs = L2*midFs; % 96 kHz
fint  = fdesign.interpolator(L2,'Halfband','TW,Ast',TW,Ast,outFs);
hint  = design(fint,'equiripple','SystemObject',true);
```

The transition width is chosen to be 8 kHz because the signal sampled at 48 kHz spans the audio band between 0 and 20 kHz. The signal has a spectral replica between 28 kHz and 68 kHz (40 kHz bandwidth centered around 48 kHz). Therefore, the transition band of the interpolator occupies the "don't care" band between 20 kHz and 28 kHz.

Analysis of the Filters Involved in the Conversion

To understand the role of each filter, it is useful to focus on the band between 0 and 96 kHz.

```hfv = fvtool(hsrc,hint);
hfv.FrequencyRange        = 'Specify freq. vector';
hfv.FrequencyVector       = linspace(0,96e3,1e3);
hfv.NormalizeMagnitudeto1 = 'on';
legend(hfv,'Sample-rate converter','Halfband interpolator');
```

The sample-rate converter preserves the band of interest between 0 and 20 kHz. It then supresses all spectral replicas up to a very high value (160*44.1e3). The decimation by 147 results in a new spectral replica centered at 48 kHz and spanning the band between 28 kHz and 68 kHz. The role of the interpolation filter is to suppress this replica as the sampling rate is increased to 96 kHz.

Create an Audio File Writer for the Converted Signal

Once the signal has been converted to 96 kHz, you can write it back to an audio file using an AudioFilterWriter.

```% Here you create a System object to write a FLAC audio file
hfileOut = dsp.AudioFileWriter('handel.flac',...
'FileFormat','FLAC',....
'SampleRate',outFs);
```

Main Processing Loop

```while ~isDone(hfileIn)
sig = step(hfileIn);  % Read audio input
sig = step(hsrc,sig); % Convert sample-rate
sig = step(hint,sig); % Interpolate by 2
step(hfileOut, sig);  % Write output audio
end
```

```release(hfileIn);
release(hfileOut);
```

Converting the Sampling Rate from 96 KHz to 44.1 KHz

In order to convert from 96 kHz to 44.1 kHz, the reverse steps can be taken. First, a decimate-by-2 halfband filter can be used to convert from 96 kHz to 48 kHz. Next, a sample-rate converter with an interpolation factor of 147 and a decimation factor of 160 can be used to obtain the 44.1 kHz signal. A significant difference is that the transition width for both filters can now be 8 kHz. As before, the first thing is to create an AudioFileReader. Possible designs for the 2 filters are:

```% Here you create a System object to read from an audio file and
% determine the file's audio sampling rate.
frameSize = 16000;
'SamplesPerFrame', frameSize, ...
'PlayCount', Inf, ...
'OutputDataType', 'double');

fileInfo = info(hfileIn);
inFs = fileInfo.SampleRate;

% Design decimate-by-2 filter
M2    = 2;    % Decimation factor
TW    = 8e3;  % Transition width in Hz
Ast   = 100;  % Stopband attenuation in dB
fdec  = fdesign.decimator(M2,'Halfband','TW,Ast',TW,Ast,inFs);
hdec  = design(fdec,'equiripple','SystemObject',true);

% Design sample-rate converter
midFs = inFs/M2; % 48 kHz
L     = 147;     % Interpolation factor
M     = 160;     % Decimation factor
TW    = 8e3;     % Transition width in Hz
Ast   = 100;     % Stopband attenuation in dB
fsrc  = fdesign.rsrc(L,M,'Nyquist',M,'TW,Ast',TW,Ast,midFs*L);
hsrc  = design(fsrc,'kaiserwin','SystemObject',true);
outFs = midFs*L/M;

% Here you create a System object to write an OGG audio file
hfileOut = dsp.AudioFileWriter('handel_new.ogg',...
'FileFormat','OGG',....
'SampleRate',outFs);
```

Main Processing Loop

```while ~isDone(hfileIn)
sig = step(hfileIn);  % Read audio input
sig = step(hdec,sig); % Convert sample-rate
sig = step(hsrc,sig); % Interpolate by 2
step(hfileOut, sig);  % Write output audio
end
```

```release(hfileIn);