DSPRelated.com
Forums

Resampling one signal to match another

Started by Randy Yates July 19, 2013
On 7/19/13 10:37 AM, Vladimir Vassilevsky wrote:
> On 7/19/2013 11:28 AM, Randy Yates wrote: >> Rick Lyons <R.Lyons@_BOGUS_ieee.org> writes: >> >>> On Fri, 19 Jul 2013 07:41:02 -0400, Randy Yates >>> <yates@digitalsignallabs.com> wrote: >>> >>>> I have two digital signals, x1[n] at approximately 370 samples/sec, and >>>> x2[n] at approximately 400 samples/sec. >>>> >>>> How can I resample x1[n] so that it matches x2[n] without knowing the >>>> precise sample rate of either? >>> >> No. They are from an LSM330 chip - one signal is the >> acceleration at ~400 Hz, the other is the gyro data at >> ~370 Hz. > > Then it is trivial problem of combining asynchronous sensors. Take one > signal as master, interpolate other signals to it. > BTW, accelerometer and gyro signals are indeed correlated.
does he have knowledge, using a common time base, when the samples from both sensors are coming in? if so, then you're right (except it ain't as trivial as implied). it's an ASRC. Randy will have fun with sample rate conversion (we talk about this a lot here), feedback control systems (we talk about this sometimes), and then some fun code to deal with pathological conditions (like startup and getting an initial sync lock) and reading and dealing with a high-speed CPU clock to deal with the other pathological case regarding when the samples (from the two different sources) do happen to land nearly on top of one another. maybe that last pathology is only something that audio guys have to worry about when ASRCing from Fs = 44100.1 Hz to 44100.0 Hz. -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On Friday, July 19, 2013 7:41:02 AM UTC-4, Randy Yates wrote:
> I have two digital signals, x1[n] at approximately 370 samples/sec, and > > x2[n] at approximately 400 samples/sec. > > > > How can I resample x1[n] so that it matches x2[n] without knowing the > > precise sample rate of either? > > -- > > Randy Yates > > Digital Signal Labs > > http://www.digitalsignallabs.com
Randy, Do you know the one of one relative to the other accurately? Clay
On Fri, 19 Jul 2013 07:41:02 -0400, Randy Yates
<yates@digitalsignallabs.com> wrote:

>I have two digital signals, x1[n] at approximately 370 samples/sec, and >x2[n] at approximately 400 samples/sec. > >How can I resample x1[n] so that it matches x2[n] without knowing the >precise sample rate of either? >-- >Randy Yates >Digital Signal Labs >http://www.digitalsignallabs.com
If you just need them at the same rate, then a polyphase filter or other resampling filter (e.g., Farrow) can be used to interpolate samples from one rate to another. If you need the rate-matched samples time aligned, so that they can be processed as though they were sampled at the same time, then to do this offline will require some method of time stamping samples so that it is known that they actually go together. This will account for drift or innacuracies in the assumptions about the clock phases, etc. If you have the means to do this in real-time, then you can apply a Farrow or polyphase or other resampling filter and know which samples go where in time since you can use the two clocks to drive the resampling process. We do this in comm systems all the time when the symbol clock isn't related to the sampling clock. Audio rate resampling filters do this as well, and could probably be applied to this problem since the rates are so low. Eric Jacobsen Anchor Hill Communications http://www.anchorhill.com
clay@claysturner.com writes:

> On Friday, July 19, 2013 7:41:02 AM UTC-4, Randy Yates wrote: >> I have two digital signals, x1[n] at approximately 370 samples/sec, and >> >> x2[n] at approximately 400 samples/sec. >> >> >> >> How can I resample x1[n] so that it matches x2[n] without knowing the >> >> precise sample rate of either? >> >> -- >> >> Randy Yates >> >> Digital Signal Labs >> >> http://www.digitalsignallabs.com > > Randy, > > Do you know the one of one relative to the other accurately?
I think you have hit it on the head, Clay. You and Tim intimated at it as well via "side info." I don't really KNOW the ratio is accurate, but it's a pretty good bet. They are from the same chip. I would think the chip designers would have one clock source and derived all clocks from there. So I'm thinking I should be able to know that the sample rate of x2 is 20/19 of the sample rate of x1, i.e., Fs2 = 20/19 * Fs1. In that case I can use a Farrow filter or other M/L rational SRC. -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
robert bristow-johnson <rbj@audioimagination.com> writes:

> On 7/19/13 10:37 AM, Vladimir Vassilevsky wrote: >> On 7/19/2013 11:28 AM, Randy Yates wrote: >>> Rick Lyons <R.Lyons@_BOGUS_ieee.org> writes: >>> >>>> On Fri, 19 Jul 2013 07:41:02 -0400, Randy Yates >>>> <yates@digitalsignallabs.com> wrote: >>>> >>>>> I have two digital signals, x1[n] at approximately 370 samples/sec, and >>>>> x2[n] at approximately 400 samples/sec. >>>>> >>>>> How can I resample x1[n] so that it matches x2[n] without knowing the >>>>> precise sample rate of either? >>>> >>> No. They are from an LSM330 chip - one signal is the >>> acceleration at ~400 Hz, the other is the gyro data at >>> ~370 Hz. >> >> Then it is trivial problem of combining asynchronous sensors. Take one >> signal as master, interpolate other signals to it. >> BTW, accelerometer and gyro signals are indeed correlated. > > does he have knowledge, using a common time base, when the samples > from both sensors are coming in? if so, then you're right (except it > ain't as trivial as implied). it's an ASRC. > > Randy will have fun with sample rate conversion (we talk about this a > lot here), feedback control systems (we talk about this sometimes), > and then some fun code to deal with pathological conditions (like > startup and getting an initial sync lock) and reading and dealing with > a high-speed CPU clock to deal with the other pathological case > regarding when the samples (from the two different sources) do happen > to land nearly on top of one another. maybe that last pathology is > only something that audio guys have to worry about when ASRCing from > Fs = 44100.1 Hz to 44100.0 Hz.
Yes, this is the worst-case - ASRC came to my mind a bit late. Robert, read my post to Clay. I *think* the situation isn't this bad, i.e., I know Fs2/Fs1 precisely, but I'm not sure. Sometimes I think half of engineering is detective work... -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
On 7/19/13 12:13 PM, Randy Yates wrote:
> clay@claysturner.com writes: >
...
>> >> Do you know the one of one relative to the other accurately? > > I think you have hit it on the head, Clay. You and Tim intimated > at it as well via "side info." > > I don't really KNOW the ratio is accurate, but it's a pretty good > bet.
so what are the consequences if you lose the bet? in audio, it would be the occasional nasty discontinuity as some pointer into a circular buffer crosses the boundary and wraps around.
> They are from the same chip. I would think the chip designers > would have one clock source and derived all clocks from there.
that would be important knowledge to get to. if true, then there is a period when one device gets exactly 20 samples and the other device gets 19. (why would the chip designers do that? the only explanation i can think of involves lysergic acid diethylamide. what is gained by saving 1 whole sample in one channel vs. the other in that period? what is lost is pretty clear, in terms of code simplicity and predictability in performance.) -- r b-j rbj@audioimagination.com "Imagination is more important than knowledge."
On Fri, 19 Jul 2013 15:39:00 -0400, Randy Yates
<yates@digitalsignallabs.com> wrote:

>robert bristow-johnson <rbj@audioimagination.com> writes: > >> On 7/19/13 10:37 AM, Vladimir Vassilevsky wrote: >>> On 7/19/2013 11:28 AM, Randy Yates wrote: >>>> Rick Lyons <R.Lyons@_BOGUS_ieee.org> writes: >>>> >>>>> On Fri, 19 Jul 2013 07:41:02 -0400, Randy Yates >>>>> <yates@digitalsignallabs.com> wrote: >>>>> >>>>>> I have two digital signals, x1[n] at approximately 370 samples/sec, and >>>>>> x2[n] at approximately 400 samples/sec. >>>>>> >>>>>> How can I resample x1[n] so that it matches x2[n] without knowing the >>>>>> precise sample rate of either? >>>>> >>>> No. They are from an LSM330 chip - one signal is the >>>> acceleration at ~400 Hz, the other is the gyro data at >>>> ~370 Hz. >>> >>> Then it is trivial problem of combining asynchronous sensors. Take one >>> signal as master, interpolate other signals to it. >>> BTW, accelerometer and gyro signals are indeed correlated. >> >> does he have knowledge, using a common time base, when the samples >> from both sensors are coming in? if so, then you're right (except it >> ain't as trivial as implied). it's an ASRC. >> >> Randy will have fun with sample rate conversion (we talk about this a >> lot here), feedback control systems (we talk about this sometimes), >> and then some fun code to deal with pathological conditions (like >> startup and getting an initial sync lock) and reading and dealing with >> a high-speed CPU clock to deal with the other pathological case >> regarding when the samples (from the two different sources) do happen >> to land nearly on top of one another. maybe that last pathology is >> only something that audio guys have to worry about when ASRCing from >> Fs = 44100.1 Hz to 44100.0 Hz. > >Yes, this is the worst-case - ASRC came to my mind a bit late. Robert, >read my post to Clay. I *think* the situation isn't this bad, i.e., I >know Fs2/Fs1 precisely, but I'm not sure.
Even if you know the ratio, if there is one, if you want to assure that two samples go with the same time instant you still need to know the relative phases of the two clocks at each desired sample instant. This can be done in real time if the samples from each are available and, at a minimum, the arrival time of the clock edges.
>Sometimes I think half of engineering is detective work... >-- >Randy Yates >Digital Signal Labs >http://www.digitalsignallabs.com
Eric Jacobsen Anchor Hill Communications http://www.anchorhill.com
eric.jacobsen@ieee.org (Eric Jacobsen) writes:

> On Fri, 19 Jul 2013 15:39:00 -0400, Randy Yates > <yates@digitalsignallabs.com> wrote: > >>robert bristow-johnson <rbj@audioimagination.com> writes: >> >>> On 7/19/13 10:37 AM, Vladimir Vassilevsky wrote: >>>> On 7/19/2013 11:28 AM, Randy Yates wrote: >>>>> Rick Lyons <R.Lyons@_BOGUS_ieee.org> writes: >>>>> >>>>>> On Fri, 19 Jul 2013 07:41:02 -0400, Randy Yates >>>>>> <yates@digitalsignallabs.com> wrote: >>>>>> >>>>>>> I have two digital signals, x1[n] at approximately 370 samples/sec, and >>>>>>> x2[n] at approximately 400 samples/sec. >>>>>>> >>>>>>> How can I resample x1[n] so that it matches x2[n] without knowing the >>>>>>> precise sample rate of either? >>>>>> >>>>> No. They are from an LSM330 chip - one signal is the >>>>> acceleration at ~400 Hz, the other is the gyro data at >>>>> ~370 Hz. >>>> >>>> Then it is trivial problem of combining asynchronous sensors. Take one >>>> signal as master, interpolate other signals to it. >>>> BTW, accelerometer and gyro signals are indeed correlated. >>> >>> does he have knowledge, using a common time base, when the samples >>> from both sensors are coming in? if so, then you're right (except it >>> ain't as trivial as implied). it's an ASRC. >>> >>> Randy will have fun with sample rate conversion (we talk about this a >>> lot here), feedback control systems (we talk about this sometimes), >>> and then some fun code to deal with pathological conditions (like >>> startup and getting an initial sync lock) and reading and dealing with >>> a high-speed CPU clock to deal with the other pathological case >>> regarding when the samples (from the two different sources) do happen >>> to land nearly on top of one another. maybe that last pathology is >>> only something that audio guys have to worry about when ASRCing from >>> Fs = 44100.1 Hz to 44100.0 Hz. >> >>Yes, this is the worst-case - ASRC came to my mind a bit late. Robert, >>read my post to Clay. I *think* the situation isn't this bad, i.e., I >>know Fs2/Fs1 precisely, but I'm not sure. > > Even if you know the ratio, if there is one, if you want to assure > that two samples go with the same time instant you still need to know > the relative phases of the two clocks at each desired sample instant. > > This can be done in real time if the samples from each are available > and, at a minimum, the arrival time of the clock edges.
Hi Eric, I'm afraid there isn't enough information to attain that level of precision, but yes, what you describe is the ideal. We probably will adjust heuristically/experimentally. -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com
On Fri, 19 Jul 2013 15:13:35 -0400, Randy Yates wrote:

> clay@claysturner.com writes: > >> On Friday, July 19, 2013 7:41:02 AM UTC-4, Randy Yates wrote: >>> I have two digital signals, x1[n] at approximately 370 samples/sec, >>> and >>> >>> x2[n] at approximately 400 samples/sec. >>> >>> >>> >>> How can I resample x1[n] so that it matches x2[n] without knowing the >>> >>> precise sample rate of either? >>> >>> -- >>> >>> Randy Yates >>> >>> Digital Signal Labs >>> >>> http://www.digitalsignallabs.com >> >> Randy, >> >> Do you know the one of one relative to the other accurately? > > I think you have hit it on the head, Clay. You and Tim intimated at it > as well via "side info." > > I don't really KNOW the ratio is accurate, but it's a pretty good bet. > They are from the same chip. I would think the chip designers would have > one clock source and derived all clocks from there. > > So I'm thinking I should be able to know that the sample rate of x2 is > 20/19 of the sample rate of x1, i.e., Fs2 = 20/19 * Fs1. > > In that case I can use a Farrow filter or other M/L rational SRC.
If you have some easy way to test your "derived from one clock" assumption you should do so. Many microcontrollers that I know of have at least two independent clocks, one for real and one as a watchdog. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
Tim Wescott <tim@seemywebsite.really> writes:

> On Fri, 19 Jul 2013 15:13:35 -0400, Randy Yates wrote: > >> clay@claysturner.com writes: >> >>> On Friday, July 19, 2013 7:41:02 AM UTC-4, Randy Yates wrote: >>>> I have two digital signals, x1[n] at approximately 370 samples/sec, >>>> and >>>> >>>> x2[n] at approximately 400 samples/sec. >>>> >>>> >>>> >>>> How can I resample x1[n] so that it matches x2[n] without knowing the >>>> >>>> precise sample rate of either? >>>> >>>> -- >>>> >>>> Randy Yates >>>> >>>> Digital Signal Labs >>>> >>>> http://www.digitalsignallabs.com >>> >>> Randy, >>> >>> Do you know the one of one relative to the other accurately? >> >> I think you have hit it on the head, Clay. You and Tim intimated at it >> as well via "side info." >> >> I don't really KNOW the ratio is accurate, but it's a pretty good bet. >> They are from the same chip. I would think the chip designers would have >> one clock source and derived all clocks from there. >> >> So I'm thinking I should be able to know that the sample rate of x2 is >> 20/19 of the sample rate of x1, i.e., Fs2 = 20/19 * Fs1. >> >> In that case I can use a Farrow filter or other M/L rational SRC. > > If you have some easy way to test your "derived from one clock" > assumption you should do so. Many microcontrollers that I know of have > at least two independent clocks, one for real and one as a watchdog.
That is a good point Tim. I do have an easy way, and I am going to try it. That is to simply collect, and count, samples from each over a suitably "long" period of time (1 minute?), then see if the ratios match up. -- Randy Yates Digital Signal Labs http://www.digitalsignallabs.com