Forums

Sample Rate Conversion (Downsampling)

Started by Unknown December 14, 2004
Hello,

For a project which I'm working on, I have a 60Hz sinewave modulated with a
square wave at 0.5Hz, modulation is 2.72%

The signal comes sampled at 8kHz, I should downsample it to 200Hz, so M =
40. I do that in two stages, first with M1 = 10, then with M2 = 4. I
implemented polyphase structure in Simulink, and runs great. By "great" I
mean that the output signal, sampled at 200Hz, preserves the squarewave
shape perfectly.

Ths structure used in Simulink is the one in P.P. Vaidyanathan's book
"Multirate Systems and Filter Banks", figure 4.3-4 (a), page 124.

However, In my C implementation (on SHARC, BTW), the downsampled signal
looks weird. By "weird" I mean that the envelope of the output signal,
instead of being flat (square) as it is expected to be, seems more like a
low frequency sinusoid, still it is noticeable when the amplitude rises and
falls at the same frequency of the original modulating square wave.

Given this information, and the fact that Simulink implementation maps
directly to the figure in Vaidyanathan's book, I am pretty sure that I have
some mistakes in my C implementation, but I can't find them.

Could someone here please give me advice on what mistakes in implementing
the polyphase M-fold decimation filters lead to the sort of "weirdness" that
I got from my C implementation? That would be of much support in finding
what I did wrong.

If someone who desires to help me wants to have a look at the Simulink Model
that I implemented, to the output of my C (for SHARC) implementation, and/or
to my C implementation itself, please let me know, and I will be more than
pleased in sending it to you.

Kindest regards,

--
Jaime Andr�s Aranguren Cardona
jaac@nospam.sanjaac.com
SanJaaC Electronics
Soluciones en DSP
www.sanjaac.com

(Remove "nospam" from e-mail address)


Just a quick thought, one difference could be that in the Simulink case, your
8kHz sample rate is probably an _exact_ multiple of the 60Hz waveform, but in
the real-world case, it may not be.  Try modifying your Simulink example to use
a slightly different frequency such as 60.37 Hz or something and see what
happens.

You could also try simplifying the problem by downsampling just the 60Hz
waveform without the modulation.

"Jaime Andr&#2013265929;s Aranguren Cardona" <jaac@nospam.sanjaac.com> wrote in message
news:1103046565.72fb8397a7036cd0ab33545d0fe7c860@teranews...
> Hello, > > For a project which I'm working on, I have a 60Hz sinewave modulated with a > square wave at 0.5Hz, modulation is 2.72% > > The signal comes sampled at 8kHz, I should downsample it to 200Hz, so M = > 40. I do that in two stages, first with M1 = 10, then with M2 = 4. I > implemented polyphase structure in Simulink, and runs great. By "great" I > mean that the output signal, sampled at 200Hz, preserves the squarewave > shape perfectly. > > Ths structure used in Simulink is the one in P.P. Vaidyanathan's book > "Multirate Systems and Filter Banks", figure 4.3-4 (a), page 124. > > However, In my C implementation (on SHARC, BTW), the downsampled signal > looks weird. By "weird" I mean that the envelope of the output signal, > instead of being flat (square) as it is expected to be, seems more like a > low frequency sinusoid, still it is noticeable when the amplitude rises and > falls at the same frequency of the original modulating square wave. > > Given this information, and the fact that Simulink implementation maps > directly to the figure in Vaidyanathan's book, I am pretty sure that I have > some mistakes in my C implementation, but I can't find them. > > Could someone here please give me advice on what mistakes in implementing > the polyphase M-fold decimation filters lead to the sort of "weirdness" that > I got from my C implementation? That would be of much support in finding > what I did wrong.
Jaime Andr&#2013265929;s Aranguren Cardona wrote:
> Hello, > > For a project which I'm working on, I have a 60Hz sinewave modulated with a > square wave at 0.5Hz, modulation is 2.72% > > The signal comes sampled at 8kHz, I should downsample it to 200Hz, so M = > 40. I do that in two stages, first with M1 = 10, then with M2 = 4. I > implemented polyphase structure in Simulink, and runs great. By "great" I > mean that the output signal, sampled at 200Hz, preserves the squarewave > shape perfectly. > > Ths structure used in Simulink is the one in P.P. Vaidyanathan's book > "Multirate Systems and Filter Banks", figure 4.3-4 (a), page 124. > > However, In my C implementation (on SHARC, BTW), the downsampled signal > looks weird. By "weird" I mean that the envelope of the output signal, > instead of being flat (square) as it is expected to be, seems more like a > low frequency sinusoid, still it is noticeable when the amplitude rises and > falls at the same frequency of the original modulating square wave. > > Given this information, and the fact that Simulink implementation maps > directly to the figure in Vaidyanathan's book, I am pretty sure that I have > some mistakes in my C implementation, but I can't find them. > > Could someone here please give me advice on what mistakes in implementing > the polyphase M-fold decimation filters lead to the sort of "weirdness" that > I got from my C implementation? That would be of much support in finding > what I did wrong. > > If someone who desires to help me wants to have a look at the Simulink Model > that I implemented, to the output of my C (for SHARC) implementation, and/or > to my C implementation itself, please let me know, and I will be more than > pleased in sending it to you. > > Kindest regards, > > -- > Jaime Andr&#2013265929;s Aranguren Cardona > jaac@nospam.sanjaac.com > SanJaaC Electronics > Soluciones en DSP > www.sanjaac.com > > (Remove "nospam" from e-mail address) > >
You could also try a regular decimation implementation (instead of polyphase) to see whether of not it's your polyphase filtering code. Cheers, David
Just another thought.  Did you make sure you cycled through the coeffs in
the right order.
Try reversing the sequence in which you use the coeffs.
Although I am as careful as I can be, I can't tell you how many times I end
up experimenting
till I get the sequencing right.

Regards,
Dave Shaw
> > Could someone here please give me advice on what mistakes in implementing > the polyphase M-fold decimation filters lead to the sort of "weirdness"
that
> I got from my C implementation? That would be of much support in finding > what I did wrong. > > If someone who desires to help me wants to have a look at the Simulink
Model
> that I implemented, to the output of my C (for SHARC) implementation,
and/or
> to my C implementation itself, please let me know, and I will be more than > pleased in sending it to you. >
Good suggestion.  For some reason, I usually find the order is backwards from
what I would intuitively think!  There was an interesting thread on this a while
back:
http://tinyurl.com/3s6dc

"Att" <david.g.shaw@att.net> wrote in message
news:agGvd.124735$7i4.59110@bgtnsc05-news.ops.worldnet.att.net...
> Just another thought. Did you make sure you cycled through the coeffs in > the right order. > Try reversing the sequence in which you use the coeffs. > Although I am as careful as I can be, I can't tell you how many times I end > up experimenting > till I get the sequencing right. > > Regards, > Dave Shaw > > > > Could someone here please give me advice on what mistakes in implementing > > the polyphase M-fold decimation filters lead to the sort of "weirdness" > that > > I got from my C implementation? That would be of much support in finding > > what I did wrong. > > > > If someone who desires to help me wants to have a look at the Simulink > Model > > that I implemented, to the output of my C (for SHARC) implementation, > and/or > > to my C implementation itself, please let me know, and I will be more than > > pleased in sending it to you.
"Jim Thomas" <jthomas@bittware.com> escribi&#2013265923; en el mensaje
news:10s0pegaj0r88e9@corp.supernews.com...
> Jaime Andr&#2013265929;s Aranguren Cardona wrote: > > What else could I try, guys? I'd really appreciate if you can take a
look at
> > my code, and help me find out where the mistakes can be, or provide me
with
> > some reference C code. > > > > Thanks you very much in advance, > > Did you see Grant's multirate code on dspguru.com? >
Hi Jim, Yes I did. It is not polyphase, right? I'd love it was... Any example in C with ployphase implementation? -- Jaime Andr&#2013265929;s Aranguren Cardona jaac@nospam.sanjaac.com SanJaaC Electronics Soluciones en DSP www.sanjaac.com (Remove "nospam" from e-mail address)
"Jaime Andr&#2013265929;s Aranguren Cardona" <jaac@nospam.sanjaac.com> wrote in message
news:1103129550.7987b75918af9ea133251f5237596ac2@teranews...
> "Jim Thomas" <jthomas@bittware.com> escribi&#2013265923; en el mensaje > news:10s0pegaj0r88e9@corp.supernews.com... > > Jaime Andr&#2013265929;s Aranguren Cardona wrote: > > > What else could I try, guys? I'd really appreciate if you can take a > look at > > > my code, and help me find out where the mistakes can be, or provide me > with > > > some reference C code. > > > > > > Thanks you very much in advance, > > > > Did you see Grant's multirate code on dspguru.com? > > > > Hi Jim, > > Yes I did. It is not polyphase, right? I'd love it was... Any example in C > with ployphase implementation?
Yes - from what I remember it is a polyphase implementation. Cheers Bhaskar
> -- > Jaime Andr&#2013265929;s Aranguren Cardona > jaac@nospam.sanjaac.com > SanJaaC Electronics > Soluciones en DSP > www.sanjaac.com > > (Remove "nospam" from e-mail address) > >
Try this:
http://www.mega-nerd.com/SRC/
It includes source.

"Jaime Andr&#2013265929;s Aranguren Cardona" <jaac@nospam.sanjaac.com> wrote in message
news:1103129550.7987b75918af9ea133251f5237596ac2@teranews...
> "Jim Thomas" <jthomas@bittware.com> escribi&#2013265923; en el mensaje > news:10s0pegaj0r88e9@corp.supernews.com... > > Jaime Andr&#2013265929;s Aranguren Cardona wrote: > > > What else could I try, guys? I'd really appreciate if you can take a > look at > > > my code, and help me find out where the mistakes can be, or provide me > with > > > some reference C code. > > > > > > Thanks you very much in advance, > > > > Did you see Grant's multirate code on dspguru.com? > > Hi Jim, > > Yes I did. It is not polyphase, right? I'd love it was... Any example in C > with ployphase implementation?
Jaime Andr&#2013265929;s Aranguren Cardona wrote:
> "Jim Thomas" <jthomas@bittware.com> escribi&#2013265923; en el mensaje > news:10s0pegaj0r88e9@corp.supernews.com... > >>Jaime Andr&#2013265929;s Aranguren Cardona wrote: >> >>>What else could I try, guys? I'd really appreciate if you can take a > > look at > >>>my code, and help me find out where the mistakes can be, or provide me > > with > >>>some reference C code. >>> >>>Thanks you very much in advance, >> >>Did you see Grant's multirate code on dspguru.com? >> > > > Hi Jim, > > Yes I did. It is not polyphase, right? I'd love it was... Any example in C > with ployphase implementation?
The interpolation code on dspguru is most definitely polyphase. My understanding is that polyphase only comes into play during interpolation. It's only during interpolation that you get the opportunity to skip through the coefs - essentially choosing a phase of the impulse response. In decimation you work on contiguous samples, but only calculate the outputs you're going to keep. Am I wrong? -- Jim Thomas Principal Applications Engineer Bittware, Inc jthomas@bittware.com http://www.bittware.com (603) 226-0404 x536 Hope springs occasionally.
Jim Thomas <jthomas@bittware.com> writes:

> Jaime Andr&#2013265929;s Aranguren Cardona wrote: > > "Jim Thomas" <jthomas@bittware.com> escribi&#2013265923; en el mensaje > > news:10s0pegaj0r88e9@corp.supernews.com... > > > > >>Jaime Andr&#2013265929;s Aranguren Cardona wrote: > >> > >>>What else could I try, guys? I'd really appreciate if you can take a > > look at > > > > > >>>my code, and help me find out where the mistakes can be, or provide me > > with > > > > > >>>some reference C code. > >>> > >>>Thanks you very much in advance, > >> > >>Did you see Grant's multirate code on dspguru.com? > >> > > Hi Jim, > > > Yes I did. It is not polyphase, right? I'd love it was... Any > > example in C > > > with ployphase implementation? > > The interpolation code on dspguru is most definitely polyphase. > > My understanding is that polyphase only comes into play during interpolation. > It's only during interpolation that you get the opportunity to skip > through the coefs - essentially choosing a phase of the impulse > response. In decimation you work on contiguous samples, but only > calculate the outputs you're going to keep. > > > Am I wrong?
Well, I hate to say wrong because I see exactly where you're coming from and I like to view it your way, but according to Mitra [1] the term "polyphase," and the corresponding partitioning of filter coefficients, can be applied to both interpolators and decimators. In a polyphase M-fold interpolator, you have M polyphase filter coefficient sets E_0 through E_{M-1} that each operate at the incoming sample rate. Each are then upsampled by M, delayed, and then summed. The end result is that, assuming the total filter length is M*K (so that each polyphase section is K coefficients long), your computations require M*K*Fs multiplications/second instead of M^2*K*Fs, where Fs is the input sample rate. In a polyphase M-fold decimator, you have M polyphase filter coefficient sets E_0 through E_{M-1} that each operate at the outgoing sample rate. Each section is fed from an M-fold downsampler. M phases of the input signal are obtained at the M downsampler outputs by delaying the input of each by a sample. Then each polyphase section is convolved and the results summed. Again your computations require M*K*Fs multiplications/second instead of M^2*K*Fs, where Fs is the output sample rate. -- Randy Yates Sony Ericsson Mobile Communications Research Triangle Park, NC, USA randy.yates@sonyericsson.com, 919-472-1124