Technical notes on using Analog Devices DSPs, processors and development tools
Contact our technical support at dsp.support@analog.com and at dsptools.support@analog.com
Or vi sit our o n-li ne r esou rces htt p:/ /www.analog.com/ee-notes and http://www.analog.com/processors
Rational Sample Rate Conversion with Blackfin® Processors
Contributed by Jeff Sondermeyer, Senior DSP FAE Rev 4 – March 18, 2004
Introduction
The process of converting the sampling rate of a signal
from one rate to another is called sampling rate conversion
(or SRC). This technique is encountered in many
application areas such as:
• Digital Audio (the focus of this paper)
• Communications systems
• Speech Processing
• Antenna Systems
• Radar Systems
Sampling rates may be changed upward or downward.
Increasing the sampling rate is called interpolation, and
decreasing the sampling rate is called decimation.
Reducing the sampling rate by a factor of M is achieved by
discarding every M-1 samples, or, equivalently keeping
every M’th sample. Increasing the sampling rate by a
factor of L (interpolation by factor L) is achieved by
inserting L-1 zeros into the output stream after every
sample from the input stream of samples.
This system can perform SRC for the following cases:
reduction of the sampling rate and interpolation is the
increasing of the sample rate.
Decimation
A reduction of sample rate (decimation) by a factor of M is
achieved by sequentially discarding M-1 samples and
retaining every M’th sample. While discarding M-1 of
every M input samples reduces the original sample rate by
a factor of M, it also causes input frequencies above one-
half the decimated sample rate to be aliased into the
frequency band from DC to the decimated Nyquist
frequency. To mitigate this effect, the input signal must be
lowpass filtered to remove frequency components from
portions of the output spectrum which are required to be
alias free in subsequent signal processing steps. A benefit
of the decimation process is that the lowpass filter may be
designed to operate at the decimated sample rate, rather
than the faster input sample rate, by using a FIR filter
structure, and by noting that the output samples associated
with the M-1 discarded samples need not be computed.
• Decimation by a factor of M
• Interpolation by a factor of L
• SRC by a rational factor of L/M.
SRC by L/M requires performing an interpolation to a
sampling rate which is divisible by both L and M. The final
output is then achieved by decimating by a factor of M.
Appropriate lowpass filtering is required to prevent both
imaging and aliasing. This system employs the polyphase,
multistage technique in the process of the sampling rate
conversion for computational savings.
1.1 Sample Rate Conversion
Designs
SRC designs use the basic properties of decimation and
interpolation to change sampling rates. Decimation is the
Copyright 2004, Analog Devices, Inc. All rights reserved. Analog Devices assumes no responsibility for customer product design or the use or application of
customers’ products or for any infringements of patents or rights of others which may result from Analog Devices assistance. All trademarks and logos are property
of their respective holders. Information furnished by Analog Devices Applications and Development Tools Engineers is believed to be accurate and reliable, however
no responsibility is assumed by Analog Devices regarding technical accuracy and topicality of the content provided in Analog Devices’ Engineer-to-Engineer Notes.
Interpolation
An increase in sample rate (interpolation) by a factor of L
is achieved by inserting L-1 uniformly spaced, zero value
samples between each input sample. While adding L-1 new
samples between each input sample increases the sample
rate by a factor of L, it also introduces images of the input
spectrum into the interpolated output spectrum at
frequencies between the original Nyquist frequency and
the higher interpolated Nyquist frequency. To mitigate this
effect, the interpolated signal must be lowpass filtered to
remove any image frequencies which will disturb
subsequent signal processing steps. A benefit of the
interpolation process is that the lowpass filter may be
designed to operate at the input sample rate, rather than the
faster output sample rate, by using a FIR filter structure,
and by noting that the inputs associated with the L-1
inserted samples have zero values.
a
Sample rate changes using both
interpolation and decimation
When the specified SRC factor is not an integer factor,
SRC design uses interpolation to increase the sample rate
to a rate which is divisible by both the input and final
output sample rates. This interpolation is then followed by
decimation to achieve the specified output rate. Note that
the output sample rate may be faster or slower than the
original input rate. In cases where both interpolation and
decimation are performed in tandem it is possible to
combine the anti-imaging filter of the interpolator and the
anti-aliasing filter of the decimator into a single filter
which satisfies both requirements. The filters which run at
the low data rate are actually implemented as a particular
structure known as a polyphase filter, which will be
discussed shortly.
1.2 Decimation
If the sampling rate is decreased by a factor M, in order to
avoid aliasing, a lowpass filter is needed with the specific
restrictions that the ratio of the half sample frequency to
the passband frequency must be less than or equal to M.
Let x(m) be the input signal, h(k), 0 <= k < K, be the
coefficients of a given lowpass filter and z(m) be the
output signal before decimating by a factor M, then:
K
−
kmxkh
z(m) = (1)
Now let the output signal after the decimator be y(r) =
z(rM) where the sampling rate is reduced by a factor M.
Clearly, y(r) = z(rM) if the output signal is decimated by a
factor M.
y(r) =
Looking carefully at this equation, one can see that the
filter is in effect using the downsampled signal. Thus the
operations of downsampling and lowpass filtering have
∑
k
K
∑
=
k
=
0
0
)()(
−
krMxkh
)()(
(2)
been embedded in such a way that the lowpass filter is
operating at the reduced data rate and the average number
of computations to generate one output sample is reduced
by M.
1.3 Interpolation
Given an incoming sample rate of F
factor of L, then the resulting output sampling frequency is
= L*F
F
out
output signal is required such that the cutoff frequency is
/2.
F
in
Let x(n) be the original input sequence, v(n) the sequence
with L-1 zeros inserted, y(n) the output sequence of the
lowpass filter and let h(0), ..., h(K-1) be the coefficients of
the lowpass filter, then:
y(n) = (3)
However, v(n-k) = 0 unless n-k is a multiple of L, since L1 zeros were inserted in the sequence x(n) to get v(n).
Again let x(n) be the input signals, and h(k) be the filter
coefficients. Then the output signal y(r) has a simple
formula:
y(r) = (4)
The average number of computations during one sampling
time is reduced by L, the interpolation factor.
. To prevent imaging, a lowpass filter on the
in
K
−
knvkh
∑
=
k
∑
=
n
0
LK
/
−
0
)()(
nxLnrh
)()(
and an interpolation
in
1.4 Sample Rate Conversion by
Rational Factor L/M
To perform sample rate conversion by a rational factor
L/M, the incoming signal is first interpolated by a factor M.
The interpolation must be performed first to preserve the
spectral content of the signal. Graphically, this process can
be represented by the following diagram:
x(n)
Sampling rate
Fx
Figure 1. Block Diagram of a Rational SRC
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 2 of 26
Up Sampler
by L
Anti-imaging
lowpass filter
sample rate LFx
Anti-Aliasing
lowpass filter
Down
Sampler
by M
y(m)
Sampling rate
(L/M)Fx
a
The anti-aliasing and anti-imaging lowpass filters can be
combined into a single low-pass filter.
1.5 Polyphase Filters
Polyphase filters are used to implement multirate filters.
The polyphase filters for interpolation-only and
decimation-only filters have a simpler structure than the
polyphase filter used between an interpolator and a
decimator.
1.5.1 Interpolator-Only Polyphase Filters
The computational efficiency of the Interpolator filter
structure can also be achieved by reducing the large FIR
filter of length K into a set of smaller filters. These smaller
filters will have a length N = K/L, where K is selected to
be a multiple of L. Since the interpolation process inserts L
- 1 zeros between successive values of x(n), only N out of
the K input values stored in the FIR filter at any one time
are nonzero. At one time instant, these nonzero values
coincide and are multiplied by the filter coefficients
h(0),h(L), h(2L),...,h(K - L). In the following instant, the
nonzero values of the input sequence coincide and are
multiplied by the filter coefficients h(1), h(L + 1), h(2L +
1),...h(K - L + 1), and so on. This observation leads us to
define a set of smaller filters called polyphase filters, with
unit sample responses:
pk (n)=h(k+nL) k = 0,1,...,L – 1
n = 0,1,...,N – 1 (5)
where N = K/L is an integer.
Additional insight can be gained about the characteristics
of the set of polyphase subfilters by noting that p
obtained from h(n) by decimation with a factor L.
Consequently, if the original filter frequency response
H(w) is flat over the range each of the polyphase subfilters
will possess a relatively flat response over the range (i.e.
the polyphase subfilters are basically allpass filters and
differ primarily in their phase characteristics). This
explains the reason for the term “polyphase” in describing
these filters. The polyphase filter can also be viewed as a
set of L subfilters connected to a common delay line.
Ideally, the kth subfilter will generate a forward time shift
of (k/L)F
subfilter. Therefore, if the zeroth filter generates zero
delay, the frequency response of the kth subfilter is:
pk(w) =e
in, for k = 0, 1 2,..., L - 1, relative to the zeroth
k
L
jw
(6)
(n) is
k
1.5.2 Decimator-Only Polyphase Filters
By transposing the interpolator structure we obtain a
commutator structure for a decimator that is based on the
parallel bank of polyphase filters. The unit sample
responses of the polyphase filter are now defined as:
pk(n) = h(k+nM) k = 0,1,...,M - 1
n = 0,1,...,N – 1 (7)
where N = K/M is an integer when K is selected to be a
multiple of M. The commutator rotates in a counterclockwise direction starting with filter p
1.5.3 Simultaneous Interpolator and Decimator
Polyphase Filter
A Polyphase filter which is used to perform lowpass
filtering between an interpolator and decimator function is
more complicated than the structures previously discussed
for either the Decimator-Only or Interpolator-Only phases.
In the Interpolator-Only case, one input leads to several
outputs, and in the Decimator-Only case, many inputs lead
to a single output. Thus, there is a relatively simple
relationship between the polyphase subfilters and h(n), the
lowpass filter coefficients. An interpolator of L samples
followed by a decimator of M samples means that L input
values must lead to M output values.
y(m) is the output of the polyphase filter
g(n,m) is the polyphase filter coefficients
h(n) is the lowpass filter used for both antiimaging of the interpolator and anti-aliasing of the
decimator
(n).
0
[x] denotes the largest integer in x (8)
mM
g(n,m) = h(nL +mM - [
]* l)
L
n=0,…,N-1 and m=0,…,L-1 (9)
y(m) =
−=10N
∑
n
g(n,m-[
M
] L) x([•
L
mM
L
]-n)
where K is the filter length of h(n) and L|K
with N = K/L, m=0,…,L-1 (10)
In a multistage implementation, this type of polyphase
filter is used between the interpolator and the decimator
stage. All other stages are either simple decimation or
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 3 of 26
a
interpolation stages. The polyphase filters are exactly those
described in Section1.5.1 and Section1.5.2. An excellent
discussion of this topic is available in Chapter 10 of
Digital Signal Processing by Proakis and Manolakis.
1.6 Polyphase, Multistage Filter
Design
Given an input sampling frequency F
output sampling frequency F
frequency F
of both F
is the Least Common Multiplier (or LCM)
min
and F
in
. The decimator of the sample rate
out
conversion is defined as D = F
is defined as U = F
/ F
min
(integer), then the smallest
out
min
. The number of primes in the
in
decimator is the maximum number of stages in the
decimation structure design. If the decimator is 24= 2 * 2 *
2 * 3, then the maximum number of stages is 4. Likewise,
the number of primes in the interpolator is the maximum
number of stages in the interpolation structure design. Thus
it is possible to have a different optimum multirate
structure for a multistage decimation structure as opposed
to a multistage interpolation structure.
If you choose M = D, L = U, then you are in a design of a
SRC system (U/D), but you can also choose M = RD, and
L = RU to get an equivalent system (RU/RM) for any
positive integer R. The user can choose R = 1, 2, 4,...
A design of a SRC requires the selection of a structure:
decimation or interpolation, over-sample rate R = 1, 2,
4,…, number of stages, a factor for each stage, and a
lowpass filter for each stage. The product of all the stage
factors should be equal to the decimator if a decimation
structure is selected or interpolator if an interpolation
structure is selected, times the over-sample rate R.
Momentum Data Systems (MDS) has developed a program
to create and optimize SRC structures and generate
coefficients: Advanced QED Series Sample Rate
Conversion System (Windows 95/NT Version only) Version
www.mds.com). This program has two methods for
2.2. (
best design of decimation and interpolation structures:
minimizing the sum of filter lengths, and minimizing the
number of computations of the signal filtering. The number
of computations is calculated as follows:
If U1, U2 and U3 are up-sample factors for a 3-stage
interpolation structure, and L1, L2 and L3 are the filter
lengths for 3-stages respectively, then the number of
computations is
This design problem is not a single-objective optimization
problem. The number of computations, the number of filter
taps and the complexity of the multi-structure enter in the
calculations. The problem becomes particularly
complicated if the number of stages is greater than 3.
This EE-Note used the QED Series Sample Rate Conversion System to determine the optimum SRC
structures and all coefficients.
1.7 SRC Code Overview
The work described in this EE-Note was based on the
principles discussed in Section 1.1 through Section 1.6.
From this, a polyphase multistage SRC was implemented
on the ADSP-BF535 Blackfin® Processor.
A zip file (SRC.zip) containing the VisualDSP++™ 3.1
projects discussed here can be obtained from Analog
Devices (
imported into later versions of VisualDSP++. The Default
C Linker Description File (*.ldf) for the latest version of
VisualDSP++ should be used to recompile/relink these
projects. Make sure BUFIN is defined in the assembly
options (see Section 1.7.3). The SRC and main program C
shell (SRC.c) were developed using the ADSP-BF535 EZ-
KIT Lite™ Evaluation Platform. The C shell contains
function calls and routines to initialize the state of the
ADSP-BF535 as well as the SRC. Since this code does not
use any DMA capabilities or peripherals, this ‘core’ code
should port directly to next generation ADSP-BF5xx
Blackfin Processors. All code for this project is listed in
the Appendix.
The following were the design objectives used in
developing the SRC functions:
• The optimized assembly routines are to be C callable
• All input and output data should be 16 bits.
• All intermediate calculations should be 32-bit double-
• All filter coefficients should be 32-bit.
• All filters were designed for audio applications with
0.2dB passband ripple
58dB stopband ripple
• The MIPS budget should be ≤ 2 MIPS for all SRC
www.analog.com). These files can be easily
(See src_init.asm and src_flt.asm in the Appendix).
precision (maintaining 31.5 bits of precision per MAC).
these criteria:
examples.
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 4 of 26
The program assumes input data comes from a 16-bit
buffer (initialized as ‘x’ in the shell). This data is copied
into a 32-bit buffer ‘in1’ within src_flt.asm. At the end of
src_flt.asm, the last 32-bit buffer ‘inx’ (where ‘x’ is the last
stage) is copied into a 16-bit buffer (‘y’ in the shell). These
16-bit input/output buffers can be eliminated to conserve
data space. In this case, you will need to undefine
‘BUFIN’ and preload 'in1' with 32-bit data and then use
the 32-bit output data from ‘inx’.
The filters were designed to convert between selected
standard audio sample rates (Hz): 48000, 44100, 32000,
22050, 16000, 11025, and 8000. See Figure 1 for the audio
SRC matrix. Note that an ‘x’ in the matrix denotes that the
SRC filter was designed and is included in SRC.zip. If you
have the SRC program from MDS (or similar) you can
generate coefficients for any SRC. See Section 1.7.2
below.
Figure 3. ‘x’ Input Data for 44.1KHz sampling of a
250Hz sine wave.
a
Figure 2. Audio SRC Matrix
The #2 workspace in this project has all the necessary plots
of the input/output stages as well as the intermediate
buffers. You can look at the data in the time domain or
apply the VisualDSP++ built-in FFT plotting function to
analyze the frequency domain. Load
‘plots_xxxxtoxxxx.vdw’ for a particular SRC.
A ‘SINE_xxxxx_16bit_1024.dat’ input file was generated
to test every SRC. This is a 16-bit, 1024-sample, 1KHz or
250Hz sine wave at the input sample rate. These input files
were generated using MATLAB® scripts (see
‘gen_sine_wave_comma_16.m’). It's easy to verify proper
SRC functionality by counting samples in one period at
both the input rate (in the ‘x’ plot) and the output rate (in
the ‘y’ plot) in workspace #2. See Figure 2 and Figure 3.
Figure 4. 'y' Output Data for 48KHz SRC of a
250Hz sine wave
The built-in FFT plotting functions were also used to
analyze input and output data. See Figure 4 and Figure 5.
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 5 of 26
Figure 5. FFT of 'x' Input Data at 250Hz
Figure 6. FFT of 'y' Output Data at 250Hz
1.7.1 Input/Output Data Sizes and the GCD
The size of NINPS and NOUTS can be modified in each
‘src_xxxxtoxxxx.h’ file (see example of src_441to48.h in
the Appendix). This will allow the user to vary the size of
the input/output buffers according to system block
processing needs. It was envisioned that the end
application would be operating on blocks of audio samples.
Note that the smallest block size can be no less than the
LCM discussed in Section 1.6. However, an integer
multiple of the LCM can be applied to increase the
a
processed block size. The user can increase or decrease the
integer multiple of the LCM (or Greatest Common
Denominator, GCD, in Table 2) by changing the buffer
sizes NINPS and NOUTS. These two numbers must be at
least half of the greatest filter coefficient count times the
INTPx to ensure valid output data. Table 1 was generated
Table 1. Greatest Common Denominator for Audio
SRC
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 6 of 26
a
1.7.2 Coefficient Generation and Formatting
Assuming a program that is similar to the MDS tool is
used, some data formatting must be performed. The
following must be done to convert the raw decimal filter
coefficients. With MDS, a *.dsp file is produced. Table 2
is an example of the MDS data format for the *.dsp file.
This file must be properly formatted as a 32-bit
hexadecimal VisualDSP++ input data file (*.dat). This is
then read (by VisualDSP++) into the corresponding
variable at initialization:
a. Use Microsoft Excel to import the *.dsp file (space
delimited). Select the "D" column and erase everything
but the decimal filter coefficients. Save the file as a
b. Use the included MATLAB® script ‘dec_file_to_hex_file_converter.m’. This script will read in decimal
(exponential) data from the *.prn file and convert to a
32-bit Hexadecimal format (*.dat file) suitable to be read
by VisualDSP++ within a data initialization section. This
MATLAB® script can be easily modified for other
formats.
1.7.3 BUFIN Define
When ‘BUFIN’ is undefined (under VisualDSP++:
PROJECT OPTIONS / ASSEMBLER / ADDITIONAL
OPTIONS: ‘-D BUFIN’), the SRC program assumes that buffer ‘in1’ is preloaded with 32-bit input data AFTER the
src_init is accomplished (buffer zeroing). This requires that
the shell program preload ‘in1’ from a 32-bit source.
Define ‘BUFIN’ to include the 16-bit buffer transfer code
within src_flt.asm. ‘x’ and ‘y’ 16-bit input buffers are not
necessary for a final application but they do allow for
easier data manipulation for test purposes.
1.7.4 Zeroing Filter Delays
To "zero" out filter delays, use the following equations as
offsets to first valid output data:
See the constants generated in the 'src_xxxxtoxxxx.h’ files.
DOFSx is actually the offset from the end of the buffer.
Therefore it is the number of valid output data samples.
This will determine how often this routine needs to be
executed in a block processed system. Be careful with this
number. The preprocessor in VisualDSP++ will not
generate fractional constants. Therefore, depending on the
math here, DOFSx could have an error of ±1 sample. For a
particular SRC, check the first sample in ‘y’ and adjust the
DOFSx accordingly.
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 7 of 26
a
1.7.5 Reducing Intermediate Buffers
One idea to reduce the number of intermediate buffers is to
implement a ‘zero_buf’ function (not included) that would
re-zero the buffers between filter sections. This would
reduce the number of intermediate buffers to two at the
expense of more MIPS to accomplish the SRC. However,
the MIPS increase would be negligible and is on the order
of the size of the buffer times the number of times it is
zeroed. These two intermediate buffers should be sized to
the maximum needed for any SRC.
1.7.6 Restrictions
If there is a large interpolation constant INTPx, this
severely reduces the number of valid data samples in the
final output buffer. For example, in the 44.1K to 48K SRC,
there is an interpolation constant of 16 in the 3rd stage. If
we only use L1 data sections (max = 4096 bytes) we only
get 111 valid data samples in the final output buffer.
However, if we can use L2 (like what is available in the
ADSP-BF535) and make this intermediate buffer as large
as 4096 words (16K bytes), we can get a relatively large
number of valid output data samples. Depending on
interpolation constants and the need to run out of single
cycle L1 memory, the limiting factor appears to be the L1
section size. We can maximize all the filters based on this
L1 section size (4096 bytes or 1024 32-bit words) or
assume we can use L2 (internal or external) and make the
intermediate buffers larger. In the latter case, the number
of valid output data samples greatly increases.
1.7.7 Unresolved Issues
The following SRCs produced corrupted output data when
using a 3-stage interpolator structure:
passband ripple = 0.0001 and a stopband ripple = 98dB.
This provided a overall SNR of 90dB through all 3 stages
of the filter. This was tested using Cooledit 2000 software.
If a lesser system SNR is desirable (50-70dB), a 32-bit
implementation will provide a SNR that is close to the
stopband attenuation. For higher system SNR's (above
90dB), much higher stopband attenuations are required.
Conclusions
The code and filters in this EE-Note were generated
specifically with audio SRC in mind. Notwithstanding, by
generating new filter input files with tighter passband and
stopband ripple, this code could be used unaltered for
many different applications. Keep in mind that we gained
computational efficiency by eliminating the LxL multiply
and thus only retaining 31.5 bits of precision for each
Multiply And Accumulate (MAC). This amount of
precision is more than enough for most applications.
Notice in src_flt.asm that the inner MAC loops are only 2
cycles, enabling double precision math with very little
overhead. This “low cycle” double precision capability of
Blackfin™ is one of the great advantages of this
architecture over competing single MAC architectures.
Placement of data and code sections (i.e. L1, internal or
external L2) is up to the user. However, coefficients and
data should be placed in separate banks to avoid stalls
(only applies to L1). Also, whether cache or SRAM is used
will greatly impact the overall cycle counts. Since there are
many filters required for all the various audio SRC
combinations, it was assumed that coefficients would be
placed in a larger external L2 SRAM or SDRAM. These
coefficients could either be cached internally or brought
into L1/L2 via DMA concurrent to block processing.
11025to16,
16to2204, and
8to11025
Therefore, a 2-stage filter decimator structure was used
instead and produced valid results. It appears that the MDS
filter generator produced corrupted 3rd stage outputs for all
SRCs that up-converted between two similar rates. The
MDS program chose by default a 3-stage interpolator
structure in each of these cases.
1.7.8 Case Study of Total SNR
Two common SRC changes are 44.1KHz to 48KHz and
48KHz to 44.1KHz. Instead of using the stopband and
passband ripples above, a filter was generated with
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 8 of 26
Recommendations for Further Development
The code developed in this EE-Note can be applied to any
application requiring SRC. For example, many video
applications require the ability to scale images to change
the video size (D1 to CIF, etc.). This Polyphase multistage
SRC approach could be modified to work on byte-wide
single precision video data. The basic structure of this code
would not require many alterations. Instead of working on
one time-domain double-precision data sample per cycle,
the SRC would be modified to operate on two byte-wide
frequency-domain data samples per cycle.
Finally, the code has not been completely optimized.
Improvements can be made to reduce overall cycles
particularly the elimination of pipeline stalls.
a
Appendix
SRC.c
/****************************************************************************
* File: SRC.c
* Date Started: Sept 26 2002
* Created: Jeff Sondermeyer
****************************************************************************/
/*
(C) Copyright 2002 - Analog Devices, Inc. All rights reserved.
File Name: SRC.c
Date Modified: 3/12/2004 Jeff Sondermeyer Rev 0.3
Purpose: The Sample Rate Converter (SRC) and Main Program Shell Developed using the ADSP-21535 EZ-KIT
Lite Evaluation Platform. This C shell contains function calls and routines to initialize the state of
the 21535 as well as the SRC. This program assumes input data comes from a 16-bit buffer (initialized
as 'x' in this shell). This data is copied into a 32-bit buffer 'in1' within src_flt.asm. At the end
of src_flt.asm, the last 32-bit buffer 'inx' (where 'x' is the last stage) is copied into a 16-bit
buffer ('y' in this shell). These 16-bit input/output buffers can be eliminated to conserve data
space. In this case, you will need to undefine 'BUFIN' and preload 'in1' with 32-bit data and then
use the 32-bit output data from 'inx'. The converter was designed to convert between any of the
following rates: 48000, 44100, 32000, 22050, 16000, 11025, and 8000. If you have the SRC program from
Momentum Data Systems you can generate coefficients for any SRC. Follow #3 below. The #2 workspace in
this project has all the necessary plots of the input/output stages as well in the intermediate
buffers. You can look at the data in the time domain or apply the built-in FFT plotting function to
analyze the frequency domain. Load "plots_xxxxtoxxxx.vdw" for a particular SRC. I have generated a
"SINE_xxxxx_16bit_1024.dat" input file to test every SRC. This is a 16-bit, 1024-sample, 1KHz sine
wave at the input sample rate. These were generated using MATLAB (see 'gen_sine_wave_comma_16.m').
It's easy to verify proper conversion by counting samples in one period at both the input rate (in the
'x' plot) and the output rate (in the 'y' plot) in workspace #2.
Notes:
1. You can modify the size of NINPS and NOUTS in each 'src_xxxxtoxxxx.h' file. However, it MUST be
the same multiple of the GCD.
2. Buffer sizes, NINPS and NOUTS must be at least half of the filter coefficient sizes times the INTPx
value to ensure valid output data.
3. Do the following to convert the decimal filter coefficients from Momentum Data Systems SRC *.dsp
file to properly format this data as 32-bit Hexidecimal value. This is then read into the
corresponding variable at initialization:
a. Use Excel to import the *.dsp file (space delimited). Select the "D" column and erase everything
else. Save the file as a "Formatted Text (Space Delimited)(*.prn)" file.
b. Use the MATLATB program "dec_file_to_hex_file_converter.m". This MATLAB program will read in
decimal (exponential) data from a file (*.prn) and convert to a 32-bit Hexidecimal format (*.dat file)
suitable to be read by VisualDSP within a data initialization section.
4. When 'BUFIN' is undefined, the program assumes that 'in1' is preloaded with 32-bit input data AFTER
the src_init is accomplished (buffer zeroing). This requires that the shell program preload 'in1'
from a 32-bit source. Define 'BUFIN' to include the 16-bit buffer transfer code within src_flt.asm.
x and y 16-bit buffers are nice but is just another chunk of memory that is necessary.
5. To "zero" out filter delays, use the following equations as offsets to first valid data:
1st Offset = (LENG1-1)/(2*DOWN1)
2nd Offset = INTP2/DOWN2*1st Offset + (LENG2-1)/(2*DOWN2)
3rd Offset = INTP3/DOWN3*2nd Offset + (LENG3-1)/(2*DOWN3)
See the constants generated in the 'src_xxxxtoxxxx.h' files.
6. DOFSx (in src_xxxxtoxxxx.h) is the offset and also is the number of valid output data samples.
This will allow you to figure how often this routine needs to be executed in a block-processed system.
Be careful with this number. The preprocessor in VDSP will not generate fractional constants.
Therefore, depending on the math here, DOFSx could have an error of +/-1. For a particular SRC, check
the first sample in 'y' and adjust the DOFSx accordingly.
7. One idea of reducing the number of intermediate buffers is to call a 'zero_buf' function that would
rezero the buffers between filter sections. This would reduce the number of intermediate buffers to
two at the expense of more MIPs. However, the MIPs increase would be negligable and is on the order
of the size of the buffer. These two intermediate buffers should be sized to the maximum needed for
any SRC.
8. If there is a big interpolation constant, this severely reduces the number of valid data samples in
the final output buffer. For example, in the 44.1K to 48K case, there is an interpolation constant of
16 in the 3rd stage. If we only use L1 data sections (max = 4096 bytes) we only get 111 valid data
samples in the final output buffer. However, if I use L2 and make this intermediate buffer as large as
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 9 of 26
4096 words (16K bytes), I can get a relatively large number of valid output data samples. The point
here is that.. depending on interpolation constants, the limiting factor appears to be the L1 section
size. I can maximize all my filters based on this L1 section size (4096 bytes or 1024 words) ...OR..
assume someone can use L2 and make the intermediate buffers larger. In the later case, the number of
VALID output data samples greatly increases.
9. The half band code was not implemented. Therefore, the HALFB define is not used.
10. 11025to16, 16to2204, and 8to11025 produced corrupted data with 3-stage filters. Had to use 2stages. MDS filter generator produces corrupted 3rd stage output for close sample rate conversions
that required up sampling??? Not sure why.
11. The latest revision of the code was debugged on a Momentum Systems Hawk PCI board. All FileIO was
done over the PCI bus. Several things need to change in this code to work with the Hawk board:
a. Define ‘HAWK’
b. Add idle.c and the basiccrt.s file for the Hawk board to the project.
///////////////////////////////////////////////////////////////////
// This line enables the PCI as the default device for file I/O
//#pragma retain_name
//extern int __default_io_device = PCI_IO;
//extern int __default_io_device = FILEIO;
///////////////////////////////////////////////////////////////////
I0 = dedicated to input buffer 'inx'
I1 = general use...reading 'inputData' plus others
I2 = general use...reading 'inx' for output data
I3 = general use...
Input Data Structure (VAR_SIZE words)
AIS: address of input signal (circular), updated after return,
SIS: circular size of AIS,
AOS: address of output signal (circular), updated after return,
SOS: circular size of AOS,
AFA: address of filter array,
LEN: poly-phase filter length,
UPR: up sample rate >= 2,
DNR: down sample rate = 1 is assumed
NIS: number of input signals
NOS: number of output signals
SHF: number of shift counter, 0 or 1
*/
.SECTION L1_data_a;
.align 4;
.byte4 pt_fundst; // pointer to fundamental structure
.byte4 pt2_fundst; // pointer to fundamental structure
.byte4 st_handle; // pointer to stage data handle
.byte2 inputs; // number of inputs
.byte2 outputs; // number of outputs
.byte2 diff_offset; // Offset difference
.GLOBAL _src_flt;
.SECTION program;
_src_flt:
a
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 15 of 26
[--SP]=(R7:4,P5:3); // Push R7...
P1 = R0; // Address of input data
P2 = R1; // Address of output data
P0 = [SP+40]; // Address of fundemental structure
p3.l = diff_offset;
p3.h = diff_offset;
w[p3] = r2; // save DOFS3 (difference offset to strip filter delay off of
final buffer
p3.l = pt_fundst; // p3 -> to fundemental structure
p3.h = pt_fundst;
[p3] = p0; // save fundemental stage pointer in memory pointed at by p3
#ifdef BUFIN
p5 = 24; // 6*4 = 24 bytes (post increment points to number input samples
per block)
r6 = [p0++p5];
p4 = r6; // p4 -> stage handle
r6 = [p0++]; // r6 = number of inputs samples per block
p3.l = inputs; // p3 -> to number of input samples per block
p3.h = inputs;
w[p3] = r6; // save number of input samples per block
r6 = [p0++]; // r6 = number of output samples per block
p3.l = outputs; // p3 -> to number of output samples per block
p3.h = outputs;
w[p3] = r6; // save number of output samples per block
p0 = [p4++]; // p0 -> fist data structure
/******** IPDC comment *******/
// r6 = [p0++]; // r6 -> first input buffer 'inx'
// i0 = r6; // i0 -> first input buffer 'inx'
// b0 = r6; // b0 -> base of first input circular buffer
/******************************/
/*********IPDC addition*******/
p5 = 44;
r6 = [p0++p5]; // r6 -> first input buffer 'inx'
i0 = r6; // i0 -> first input buffer 'inx'
p5=-40;
r6 = [p0++p5];
b0 = r6; // b0 -> base of first input circular buffer
/******************************/
r6 = [p0++];
r6 = r6 << 2; // double length (4 bytes per word)
l0 = r6; // l0 = first input circular buffer size 'SZINx'
go_back:
// p3.l = num_blocks;
// p3.h = num_blocks;
// r6 = w[p3]; // get number of blocks
// r6 += -1; // Decrement number of blocks
// w[p3] = r6; // save decremented number of blocks
// CC = r6 < 0;
// IF CC JUMP RETURN_TO_SENDER; // Return if less than 1 block
p3.l = inputs; // p3 -> to number of input samples per block
p3.h = inputs;
r7 = w[p3];
p5 = r7; // p5 = number of input samples per block
i1 = p1; // load i1 with address of 'inputData'
l1 = 0;
r5=m2; // load r5 with number of shifts
/******** IPDC comment *******/
// A1 = A1>>16;
/******************************/
/*********IPDC addition*******/
A1=A1>>>15;
/******************************/
A0+=A1;
A0 = ASHIFT A0 BY r5.l;
r4 = A0; // high half-word extraction with 16-bit
saturation. Rounding cntrl by
// RND_MOD. 0 = unbiased rounding
= default
// A0 = A0 >>> 1;
// R4 = A0;
UP_SAMPLE_END: [i2++] = R4; // save output into 'inx'+1
i3 = r7; // get input back at beginning of 'inx'
m3 = 4;
i3 += m3; // increment by 1 word (4 bytes)
r7 = i3; // update r7 -> 'inx' buffer
UP_SRC_OUTER_END:
r0 += -1; // Check number of input samples (NIS)
CC = r0 <= 0;
IF !CC JUMP UP_SRC_OUTER_BEGIN; // if NIS equal to 0, jump to UP_SRC_OUTER_BEGIN
a
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 18 of 26
p4 = 8; // 2 words (2*4bytes per word)
[p0++p4] = r7; // save the input signal address
r6 = i2;
[p0] = r6; // save the output signal address
UPSTAGE_END:
r2 += -1; // Check number of stages
CC = r2 <= 0;
IF !CC JUMP UPSTAGE_BEGIN; // if upstage not equal to 0, jump to UPSTAGE_BEGIN
r6 = [p0++];
r6 = r6 << 2; // double the output size (4 bytes per word)
a
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 19 of 26
l2 = r6; // l2 = Size of Output Stage (SOS)
r3 = [p0++]; // r3 -> the filter coefficients
r6 = [p0++]; // r6 = poly-phase filter size
p3 = r6; // save poly-phase into p3
r6 = [p0++]; // r6 = UPR (filter step)
r6 = r6 << 2; // post increment must be two bytes
m1 = r6; // post increment set to UPR
p4 = 8; // always skip over UPR (2*4bytes) in the up SRC
r0 = [p0++p4]; // r0 = DNR
r0 = r0 << 2; // four bytes per word
r6 = [p0++]; // r6 = NOS
p5 = r6; // p5 = Number of Outputs (NOS)
p4 = -40; // Backup 10 words (10x4)
// r1.l = w[p0]; // r1.l = Number of shifts (can be left shift=upshift or right
shift=downshift)
r6 = [p0++p4]; m2 = r6; // m2 = Number of shifts
r2 = 0; // set poly index value to 0
i1 = r3; // i1 -> filter coefficients
// CC = r6 <= 0;
// IF !CC JUMP pvt_positive; // if # of shifts > 0, jump over
// CC = r6 < 0;
// IF CC JUMP pvt_negative; // if # of shifts < 0, jump over
/*********IPDC addition*******/
A1=A1>>>15;
/******************************/
A0+=A1;
A0 = ASHIFT A0 BY r2.l;
r6 = A0; // high half-word extraction with 16-bit saturation.
Rounding cntrl by
// RND_MOD. 0 = unbiased rounding = default
[i2++] = r6; // save output into 'inx'+1
// JUMP shiftDone;
//shiftPos: // Left Shift = Up shift = positive number
// A1 = ASHIFT A1 BY r2.l;
// r6.h = A1; // high half-word extraction with 16-bit saturation.
Rounding cntrl by
// RND_MOD. 0 = unbiased rounding = default
// w[i2++] = r6.h; // save output into 'inx'+1
//shiftDone:
i3 = m1; // restore i3
DN_OUT_END:
i3 += m3; // increment by 4 bytes per word
a
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 22 of 26
r7 = i3;
p4 = 8; // 2 words (2*4bytes per word)
[p0++p4] = r7; // save the input signal address
r6 = i2;
[p0] = r6; // save the output signal address
DOWNSTAGE_END:
r0 += -1; // Check number of downstages
CC = r0 <= 0;
IF !CC JUMP DOWNSTAGE_BEGIN; // if # equal to 0, jump to DOWNSTAGE_BEGIN
return_src_core:
#ifdef BUFIN
p3.l = diff_offset; // p3 -> Offset difference to strip leading zeroes off of final
buffer
p3.h = diff_offset;
r7 = w[p3]; // r7 = number of outputs samples per block
// r7 = 147;
p5 = r7; // p5 = number of outputs samples per block (OSPB)
r7 = r7 << 2; // 4 bytes per word.
m2 = r7;
i2 -= m2; // modify i2 = i2-#OSPB (backup pointer by output block size)
i0 = r3; // i0 -> 'inx' buffer
p5 = r4;
r5 = 0;
l0 = 0; // l0 = length of 'inx' buffer SZINx
LSETUP(SET_ZERO_BEGIN, SET_ZERO_END) LC1 = p5;
SET_ZERO_BEGIN:
SET_ZERO_END: [i0++] = r5; // zero out a 32-bit word
L_END: nop;
_src_init_end1: (R7:4,P5:3)=[SP++]; // Pop R7 and P5
RTS;
a
Listing 4. initial.asm
src_441to48.h
//Include file for 44.1KHz to 48KHz. Greatest Common Denominator (GCD) = 147/160.
#define HALFB 0 // Half band flag
#define NUPST 2 // Number of up stages
#define PVTFL 1 // Pivot flag
#define NDWNS 0 // Number of down stages
#define STAGE 3 // Number of total stages
#define NINPS 147 // Number of imput samples (Should be a even multiple of
the GCD)
#define NOUTS 160 // Number of output samples (Should be the same
multiple as above of the GCD)
#define INTP1 2
Rational Sample Rate Conversion with Blackfin® Processors (EE-183) Page 24 of 26