DSPRelated.com
Forums

Re-sampling

Started by Pierre de Vos February 3, 2007
Hi All,

I need to re-sample a signal sampled at a fixed rate.  The fundamental 
frequency of the signal can be from 45-65Hz.  The re-sampling must give 2^N 
amount of samples per cycle of the fundamental.  The employed method must be 
adaptive - if the fundamental frequency changes, it must still give 2^N 
samples/Cycle for the current frequency.

Anybody have any ideas how to attack this problem?

Pierre 


Pierre de Vos wrote:

> Hi All, > > I need to re-sample a signal sampled at a fixed rate. The fundamental > frequency of the signal can be from 45-65Hz. The re-sampling must give > 2^N > amount of samples per cycle of the fundamental. The employed method must > be adaptive - if the fundamental frequency changes, it must still give 2^N > samples/Cycle for the current frequency.
Well first of all you need a sample rate converter capable of time varying conversion ratio. The Rabbit fits the bill: http://www.mega-nerd.com/SRC/ You still need to write some software around the Rabbit to detect the current frequency and modify the conversion ratio on the fly to meet your criteria. HTH, Erik -- +-----------------------------------------------------------+ Erik de Castro Lopo +-----------------------------------------------------------+ "C++ has its place in the history of programming languages. Just as Caligula has his place in the history of the Roman Empire." -- Robert Firth
Pierre de Vos wrote:
> Hi All, > > I need to re-sample a signal sampled at a fixed rate. The fundamental > frequency of the signal can be from 45-65Hz. The re-sampling must give 2^N > amount of samples per cycle of the fundamental. The employed method must be > adaptive - if the fundamental frequency changes, it must still give 2^N > samples/Cycle for the current frequency. > > Anybody have any ideas how to attack this problem?
What rate will be used for the reconstruction? I hope you realize that if there are are no sidebands, the output will never vary. How is that useful? Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
"Jerry Avins" <jya@ieee.org> wrote in message 
news:-qadnSn9SJokk1jYnZ2dnUVZ_vninZ2d@rcn.net...
> Pierre de Vos wrote: >> Hi All, >> >> I need to re-sample a signal sampled at a fixed rate. The fundamental >> frequency of the signal can be from 45-65Hz. The re-sampling must give >> 2^N amount of samples per cycle of the fundamental. The employed method >> must be adaptive - if the fundamental frequency changes, it must still >> give 2^N samples/Cycle for the current frequency. >> >> Anybody have any ideas how to attack this problem? > > What rate will be used for the reconstruction? I hope you realize that if > there are are no sidebands, the output will never vary. How is that > useful? > > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Jerry, I have to calculate the RMS to a high degree of accuracy on a mains frequency signal. I need to do this every cycle or half-cycle in real-time, therefore I must employ some method of synchronous sampling as I'm usings simpson's rule to approximate the integral in the RMS calculation. Previously I have done this with the sample rate locked to the fundamental of the mains frequency by detecting the zero crossings. This method has it's own issues: noise, harmonics can affect the period measurement. As I mentioned I need the information every cycle, synchronous sampling simplifies the problem: updating every 2^N (say 1024) samples means you are cycle accurate for the RMS calculation. I also use FIR filters for compensation of phase errors between multiple channels and if you sample synchronously, the amount of compensation stays the same, no matter the input frequency. I also need to calculate the relative angle between two channels - for this I will probably use a FFT (therefore the 2^N samples per cycle). I'm aiming at say 1024 samples/cycle output sample rate and my hardware allows sample rate up to about 250kSamples/s. Pierre
Pierre de Vos wrote:
> "Jerry Avins" <jya@ieee.org> wrote in message > news:-qadnSn9SJokk1jYnZ2dnUVZ_vninZ2d@rcn.net... >> Pierre de Vos wrote: >>> Hi All, >>> >>> I need to re-sample a signal sampled at a fixed rate. The fundamental >>> frequency of the signal can be from 45-65Hz. The re-sampling must give >>> 2^N amount of samples per cycle of the fundamental. The employed method >>> must be adaptive - if the fundamental frequency changes, it must still >>> give 2^N samples/Cycle for the current frequency. >>> >>> Anybody have any ideas how to attack this problem? >> What rate will be used for the reconstruction? I hope you realize that if >> there are are no sidebands, the output will never vary. How is that >> useful? >> >> Jerry >> -- >> Engineering is the art of making what you want from things you can get. >> &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295; > > Jerry, > > I have to calculate the RMS to a high degree of accuracy on a mains > frequency signal. I need to do this every cycle or half-cycle in real-time, > therefore I must employ some method of synchronous sampling as I'm usings > simpson's rule to approximate the integral in the RMS calculation. > Previously I have done this with the sample rate locked to the fundamental > of the mains frequency by detecting the zero crossings. This method has > it's own issues: noise, harmonics can affect the period measurement. As I > mentioned I need the information every cycle, synchronous sampling > simplifies the problem: updating every 2^N (say 1024) samples means you are > cycle accurate for the RMS calculation. > > I also use FIR filters for compensation of phase errors between multiple > channels and if you sample synchronously, the amount of compensation stays > the same, no matter the input frequency. > > I also need to calculate the relative angle between two channels - for this > I will probably use a FFT (therefore the 2^N samples per cycle). > > I'm aiming at say 1024 samples/cycle output sample rate and my hardware > allows sample rate up to about 250kSamples/s. > > Pierre
It sounds like resampling will not achieve your goals. You would need to measure the mains frequency with considerable precision, to work out the appropriate sampling rate to use. That will take time, and maybe wreck your extreme low latency goals. If you sample a 50Hz waveform, even one with a bit of harmonic content, at 250k samples/second you can get a pretty accurate measure of the RMS without resampling. In most cases all you need to do is track the cycles, and calculate the RMS between zero crossings. You may not hit exactly the zero crossing, if the mains and sampling frequencies are not harmonically related. However, the contribution of the samples near the zero crossing is small, and with 250kHz sampling you are going to be pretty close to the actual crossing. Regards, Steve
Pierre de Vos wrote:
> "Jerry Avins" <jya@ieee.org> wrote in message > news:-qadnSn9SJokk1jYnZ2dnUVZ_vninZ2d@rcn.net... >> Pierre de Vos wrote: >>> Hi All, >>> >>> I need to re-sample a signal sampled at a fixed rate. The fundamental >>> frequency of the signal can be from 45-65Hz. The re-sampling must give >>> 2^N amount of samples per cycle of the fundamental. The employed method >>> must be adaptive - if the fundamental frequency changes, it must still >>> give 2^N samples/Cycle for the current frequency. >>> >>> Anybody have any ideas how to attack this problem? >> What rate will be used for the reconstruction? I hope you realize that if >> there are are no sidebands, the output will never vary. How is that >> useful? >> >> Jerry >> -- >> Engineering is the art of making what you want from things you can get. >> &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533; > > Jerry, > > I have to calculate the RMS to a high degree of accuracy on a mains > frequency signal. I need to do this every cycle or half-cycle in real-time, > therefore I must employ some method of synchronous sampling as I'm usings > simpson's rule to approximate the integral in the RMS calculation. > Previously I have done this with the sample rate locked to the fundamental > of the mains frequency by detecting the zero crossings. This method has > it's own issues: noise, harmonics can affect the period measurement. As I > mentioned I need the information every cycle, synchronous sampling > simplifies the problem: updating every 2^N (say 1024) samples means you are > cycle accurate for the RMS calculation. > > I also use FIR filters for compensation of phase errors between multiple > channels and if you sample synchronously, the amount of compensation stays > the same, no matter the input frequency. > > I also need to calculate the relative angle between two channels - for this > I will probably use a FFT (therefore the 2^N samples per cycle). > > I'm aiming at say 1024 samples/cycle output sample rate and my hardware > allows sample rate up to about 250kSamples/s. > > Pierre
There's another approach you can take that doesn't require synchronous sampling. I'm sorry that I can't detail it, but I may be able to find out. My involvement with it predated my understanding of the algorithms, and was instead directed at squeezing the needed performance out of what would today be called a grossly inadequate processor. You won't go far wrong if you multiply the measured current and voltage, accumulate the products for a fixed time that approximates a cycle, and divide by the time. The relation to a cycle need only be approximate. As a matter of fact, we used a fixed sample rate for both 50 and 60 Hz, a magic number that was handed to us by the theoreticians. IIRC, we accounted for about 18 harmonica at 60 Hz. Of course, you will need the square root at the end. Jerry -- Engineering is the art of making what you want from things you can get.
Steve Underwood wrote:
> Pierre de Vos wrote: >> "Jerry Avins" <jya@ieee.org> wrote in message >> news:-qadnSn9SJokk1jYnZ2dnUVZ_vninZ2d@rcn.net... >>> Pierre de Vos wrote: >>>> Hi All, >>>> >>>> I need to re-sample a signal sampled at a fixed rate. The >>>> fundamental frequency of the signal can be from 45-65Hz. The >>>> re-sampling must give 2^N amount of samples per cycle of the >>>> fundamental. The employed method must be adaptive - if the >>>> fundamental frequency changes, it must still give 2^N samples/Cycle >>>> for the current frequency. >>>> >>>> Anybody have any ideas how to attack this problem? >>> What rate will be used for the reconstruction? I hope you realize >>> that if there are are no sidebands, the output will never vary. How >>> is that useful? >>> >>> Jerry >>> -- >>> Engineering is the art of making what you want from things you can get. >>> &#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533;&#65533; >> >> Jerry, >> >> I have to calculate the RMS to a high degree of accuracy on a mains >> frequency signal. I need to do this every cycle or half-cycle in >> real-time, therefore I must employ some method of synchronous sampling >> as I'm usings simpson's rule to approximate the integral in the RMS >> calculation. Previously I have done this with the sample rate locked >> to the fundamental of the mains frequency by detecting the zero >> crossings. This method has it's own issues: noise, harmonics can >> affect the period measurement. As I mentioned I need the information >> every cycle, synchronous sampling simplifies the problem: updating >> every 2^N (say 1024) samples means you are cycle accurate for the RMS >> calculation. >> >> I also use FIR filters for compensation of phase errors between >> multiple channels and if you sample synchronously, the amount of >> compensation stays the same, no matter the input frequency. >> >> I also need to calculate the relative angle between two channels - for >> this I will probably use a FFT (therefore the 2^N samples per cycle). >> >> I'm aiming at say 1024 samples/cycle output sample rate and my >> hardware allows sample rate up to about 250kSamples/s. >> >> Pierre > > It sounds like resampling will not achieve your goals. You would need to > measure the mains frequency with considerable precision, to work out the > appropriate sampling rate to use. That will take time, and maybe wreck > your extreme low latency goals. > > If you sample a 50Hz waveform, even one with a bit of harmonic content, > at 250k samples/second you can get a pretty accurate measure of the RMS > without resampling. In most cases all you need to do is track the > cycles, and calculate the RMS between zero crossings. You may not hit > exactly the zero crossing, if the mains and sampling frequencies are > not harmonically related. However, the contribution of the samples near > the zero crossing is small, and with 250kHz sampling you are going to be > pretty close to the actual crossing.
You compute RMS by dividing by time or sample count, not cycle interval, even though the samples should come close to encompassing a cycle. Zero crossings may not occur at the same time for both current and voltage, and that needs to be accounted for. Jerry -- Engineering is the art of making what you want from things you can get. &macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;&macr;
"Jerry Avins" <jya@ieee.org> wrote in message 
news:trWdnfuz_JGCz1jYnZ2dnUVZ_ternZ2d@rcn.net...
> Steve Underwood wrote: >> Pierre de Vos wrote: >>> "Jerry Avins" <jya@ieee.org> wrote in message >>> news:-qadnSn9SJokk1jYnZ2dnUVZ_vninZ2d@rcn.net... >>>> Pierre de Vos wrote: >>>>> Hi All, >>>>> >>>>> I need to re-sample a signal sampled at a fixed rate. The fundamental >>>>> frequency of the signal can be from 45-65Hz. The re-sampling must >>>>> give 2^N amount of samples per cycle of the fundamental. The employed >>>>> method must be adaptive - if the fundamental frequency changes, it >>>>> must still give 2^N samples/Cycle for the current frequency. >>>>> >>>>> Anybody have any ideas how to attack this problem? >>>> What rate will be used for the reconstruction? I hope you realize that >>>> if there are are no sidebands, the output will never vary. How is that >>>> useful? >>>> >>>> Jerry >>>> -- >>>> Engineering is the art of making what you want from things you can get. >>>> ??????????????????????????????????????????????????????????????????????? >>> >>> Jerry, >>> >>> I have to calculate the RMS to a high degree of accuracy on a mains >>> frequency signal. I need to do this every cycle or half-cycle in >>> real-time, therefore I must employ some method of synchronous sampling >>> as I'm usings simpson's rule to approximate the integral in the RMS >>> calculation. Previously I have done this with the sample rate locked to >>> the fundamental of the mains frequency by detecting the zero crossings. >>> This method has it's own issues: noise, harmonics can affect the period >>> measurement. As I mentioned I need the information every cycle, >>> synchronous sampling simplifies the problem: updating every 2^N (say >>> 1024) samples means you are cycle accurate for the RMS calculation. >>> >>> I also use FIR filters for compensation of phase errors between multiple >>> channels and if you sample synchronously, the amount of compensation >>> stays the same, no matter the input frequency. >>> >>> I also need to calculate the relative angle between two channels - for >>> this I will probably use a FFT (therefore the 2^N samples per cycle). >>> >>> I'm aiming at say 1024 samples/cycle output sample rate and my hardware >>> allows sample rate up to about 250kSamples/s. >>> >>> Pierre >> >> It sounds like resampling will not achieve your goals. You would need to >> measure the mains frequency with considerable precision, to work out the >> appropriate sampling rate to use. That will take time, and maybe wreck >> your extreme low latency goals. >> >> If you sample a 50Hz waveform, even one with a bit of harmonic content, >> at 250k samples/second you can get a pretty accurate measure of the RMS >> without resampling. In most cases all you need to do is track the cycles, >> and calculate the RMS between zero crossings. You may not hit exactly the >> zero crossing, if the mains and sampling frequencies are not >> harmonically related. However, the contribution of the samples near the >> zero crossing is small, and with 250kHz sampling you are going to be >> pretty close to the actual crossing. > > You compute RMS by dividing by time or sample count, not cycle interval, > even though the samples should come close to encompassing a cycle. Zero > crossings may not occur at the same time for both current and voltage, and > that needs to be accounted for.
A good point Jerry. In fact I need to apply the algorithm to a three phase system: phase displacement=120deg between voltage phases and any amount of degrees between current. Calculating say every 20ms could cause big errors on the current channels if you are not cycle accurate. Pierre
> > Jerry > -- > Engineering is the art of making what you want from things you can get. > &#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;&#4294967295;
Pierre de Vos wrote:

> Hi All, > > I need to re-sample a signal sampled at a fixed rate. The fundamental > frequency of the signal can be from 45-65Hz. The re-sampling must give > 2^N > amount of samples per cycle of the fundamental. The employed method must > be adaptive - if the fundamental frequency changes, it must still give 2^N > samples/Cycle for the current frequency. > > Anybody have any ideas how to attack this problem? > > Pierre
You may be able to use a phase-locked loop to synch to the fundamental and avoid being pulled in time by noise. By including a /N divider withinin the PLL you can take the sample clock from the undivided VCO, giving a constant N samples per i/p cycle. You may have to consider the PLL LPF bandwidth carefully is you need to respond quickly to abrupt i/p freq changes. You could generate a "locked" status flag to qualify/validate your other calculations. If you have "sample on demand" hardware, the above can be done in software. The divider could possibly be done in the hardware prescaler /M used to generate your ADC sample clock & interrupt if its divisor steps are not too coarse. Then, using a VDC variable duty cycle algorithm (similar to the loop engine in a Bresenham line drawing algorithm) you can increment or decrement the /M divisor until you get acceptable phase matching. The disadvantage of this is that you may get a few microSeconds of jitter around each sample time (+1..-1 clocks). It will average to zero long term error once it locks, but the jitter will remain. The higher the sample rate, the smaller the jitter. You could do some of the above in external hardware to reduce compromises. You are then free to choose freq's, divisors etc.. Jim A.
Jerry Avins wrote:

>> >> It sounds like resampling will not achieve your goals. You would need >> to measure the mains frequency with considerable precision, to work >> out the appropriate sampling rate to use. That will take time, and >> maybe wreck your extreme low latency goals. >> >> If you sample a 50Hz waveform, even one with a bit of harmonic >> content, at 250k samples/second you can get a pretty accurate measure >> of the RMS without resampling. In most cases all you need to do is >> track the cycles, and calculate the RMS between zero crossings. You >> may not hit exactly the zero crossing, if the mains and sampling >> frequencies are not harmonically related. However, the contribution >> of the samples near the zero crossing is small, and with 250kHz >> sampling you are going to be pretty close to the actual crossing. > > > You compute RMS by dividing by time or sample count, not cycle interval, > even though the samples should come close to encompassing a cycle. Zero > crossings may not occur at the same time for both current and voltage, > and that needs to be accounted for. > > Jerry
It would seem to me that you could get an accurate measurement that is independent of the mains frequency by using a zero crossings (either voltage or current, doesn't matter which as long as it is consistent) to define the sum interval, and then dividing by the number of samples taken during the interval. That way, your sampling needn't be synchronous or even adapted for different frequencies. The higher the sample frequency, the more accurate the result. You'd probably want to average the measurement for several cycles to reduce errors due ot noise.