DSPRelated.com
Forums

Re-sampling

Started by Pierre de Vos February 3, 2007
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 problem is solved by having a cycle-by-cycle account of power? Usually, power's thermal effect predominates, and thermal lag is itself an averager. Power averaged over the last second or so is sufficient for even fractional horsepower motors, and a missing or extra tenth of a cycle adjacent to a zero crossing will not introduce much inaccuracy to even a quarter second's average. Jerry -- Engineering is the art of making what you want from things you can get. ¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
On Feb 4, 11:25 am, Jerry Avins <j...@ieee.org> wrote:
> Steve Underwood wrote: > > Pierre de Vos wrote: > >> "Jerry Avins" <j...@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.
The original poster indicated he specifically needs to measure cycle by cycle, or even half-cycle by half-cycle. Power people like to make this a requirement, though I'm not clear about its purpose. The cycle interval is important for any RMS measurement which is not fairly long term. If you measure over short periods not synchronised to the cycles you will get considerable measurement to measurement jitter. Their long term average will be spot on, but the individual measurements will not. If you work between zero crossings the jitter will be very small, even if the sampling is not at an exact multiple of the mains cycle period. That's because the amplitude of the ambiguous sample in the measurement is guaranteed to be a small one. For robustness, your zero crossing detection should be tolerant of spikes on the mains, such as the sort that will happen near high power arc equipment, like welders. Regards, Steve
Pierre de Vos wrote:

(snip)

> 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.
My first thought is an analog PLL locked onto a multiple if the input frequency. With appropriate filters it should reasonably avoid noise, and lock onto real crossings. The frequency can't change too fast with the inertia of the generators behind it. Otherwise, I would probably not do synchronous sampling, but interpolate the zero crossing. Maybe a higher order interpolation, or least squares to reduce the noise effects. It might be easier to make suggestions if we knew why you needed to do this. -- glen
Steve Underwood wrote:

(snip)

> 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.
I think I agree, though I don't know yet why the OP wants to do this. To get RMS for a cycle you need the length of the cycle, the error in the result will be directly proportional to the error in the cycle length. But yes, I think it should work. Otherwise, do a decaying average of the cycle length and use that as an averaged cycle length. -- glen
Jerry Avins wrote:

(snip)

> 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.
I missed the part that said both voltage and current, but it makes some sense. Though I might say it isn't RMS anymore in that case. (Other than from the FTC there isn't any such thing as RMS power.) As I said before, I would do a decaying average of the cycle time, long enough to average out sampling noise, short enough to follow changes in mains frequency. Or use a PLL. -- glen
Pierre de Vos wrote:

(snip)

> 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.
How about computing separately half cycle current, half cycle voltage, and phase shift. Then multiply current, voltage, and cos(phase shift). -- glen
On Feb 3, 5:14 pm, Erik de Castro Lopo <e...@mega-nerd.com> wrote:
> Pierre de Vos wrote: > > > 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.
that would be a pitch detector (sometimes called a period or fundamental frequency estimator). (now time for Dmitry to pipe in.) r b-j
glen herrmannsfeldt wrote:
> Pierre de Vos wrote: > > (snip) > >> 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. > > How about computing separately half cycle current, half cycle voltage, > and phase shift. Then multiply current, voltage, and cos(phase shift).
Harmonics can create asymmetries that could be a problem for the half cycle approach. The current and voltage samples ideally need to be simultaneous and must in any case be nearly so. 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;
steveu@coppice.org wrote:
> On Feb 4, 11:25 am, Jerry Avins <j...@ieee.org> wrote: >> Steve Underwood wrote: >>> Pierre de Vos wrote: >>>> "Jerry Avins" <j...@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. > > The original poster indicated he specifically needs to measure cycle > by cycle, or even half-cycle by half-cycle. Power people like to make > this a requirement, though I'm not clear about its purpose. The cycle > interval is important for any RMS measurement which is not fairly long > term. If you measure over short periods not synchronised to the cycles > you will get considerable measurement to measurement jitter. Their > long term average will be spot on, but the individual measurements > will not. If you work between zero crossings the jitter will be very > small, even if the sampling is not at an exact multiple of the mains > cycle period. That's because the amplitude of the ambiguous sample in > the measurement is guaranteed to be a small one. For robustness, your > zero crossing detection should be tolerant of spikes on the mains, > such as the sort that will happen near high power arc equipment, like > welders.
I know. At one time I designed meters -- called "relays" -- to control power-distribution systems. Short-term action is usually taken on peak, not RMS, values. Negative sequence currents and many other anomalies can be reason to trip. 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;
"Pierre de Vos" <pierre.devos@webmail.co.za> wrote in message 
news:1170508300.436452@vasbyt.isdsl.net...
> 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 >
Thanks for all the comments. Let me give a bit more info about the application: The device I'm working on is a three phase power analiser of sorts. It is a new version af an existing device that I've done. Previously I've used a 16-bit fixed point DSP (ADSP-2186) to do the calcs. Sampling was synchronous: 4 times oversample decimated down to 128 samples/cycle. Synchronous sampling was implemented by feeding zero crossings into a fpga that measures the period against a high frequency counter. Sample rate is then generated with the use of a NCO that is updated ever cycle. The reason why I implemented it this way is that it solves a few problems having a fixed amount of samples/cycle: 1. RMS for voltage and current can be calculated every 128 samples (actually 129 for Simpsons rule) no matter the phase displacement between channels. 2. I have to cater for a variety of current sensors having varying phase errors. To compensate for these phase errors I used a fractional delay FIR filter and having the samplerate normalised to the fundamental means that varying frequency of mains have very little affect on the compensation. If you calibrate at 50Hz and calculate the FIR's coefficients, they hold true at 60Hz. 3. Phase angle determined by taking the FFT every 128 samples on all 6 channels at the same time. 4. Active power calculated by multiplying voltage and current sample pairs over 128 samples. 5. Re-active power calculated by delaying current samples by 90 degrees and then multiplying with the corresponding voltage samples over 128 samples - having a fixed amount of samples/cycle makes the 90 degree phase shift easy. As you can see there's a quite a few reasons why I chose to go for synchronous sampling as it eased a few issues. Accuracy of RMS values (avareged a little bit) was in the region of 0.01%. Power accuracy (actually enery) better than 0.05% at power factor 0.5 (60 degrees). Back to the problem at hand: I'm doing a new design and would like to achieve an accuracy (of power) in the region of 0.005% or better. I'm changing to a ADSP-21262 floating point Sharc and redesigning the analogue circuits. At the same time I want to look at the DSP algorithms to see if I can't squeeze out a bit more accuracy. Therefore the original question. The method I followed previously does introduce some jitter and I'm looking at going for fixed sampling rate this time (at a higher rate). I had to do a lot of work to implement points 1 to 6 above and would like to re-use some of the code if possible. Knowing little about multirate systems I thought that it might be possible (on the new more powerfull dsp) to manipulate the incoming samples to always have the required amount per cycle. I know that for this to work an accurate period measurement must still be made, but hoping with the new dsp I can do this in software instead of with a zero-crossing detector. Pierre