Hi all! Maybe someone here might help me with a DSP problem that should be trivial, but to me, as a newbie to DSP, is not. I'm developing a microcontroller-based speed controller for my small hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the zero crossings of the 50Hz signal coming from the generator, and measuring the duration of each half cycle, obtaining a signal at a rate of 100 samples per second. The signal is in 16 bit format, and nominally 10000 counts tall. Typical variations are a few tens of counts, and occasionally as much as 1000 counts. I'm subtracting the 10000 reference, and using the resulting error signal as input to a proportional-integral control function, whose output drives dump loads that burn off the excess output from the generator. The little problem I have is that a small 420Hz signal on the power line, apparently coming from the generator's internal voltage regulator, is causing jitter in the zero crossings, which is in turn causing the proportional function of my controller to imprint an unwanted modulation on its output. So I would like to apply a low pass filter function to my signal, with a cutoff frequency of roughly 20Hz, before the P-I function. The question is: How can I implement this DSP low pass filter? It has to be in some simple way, because neither the PIC nor I can handle overly complex math! The only thing I can come up with is averaging the last several samples, perhaps with some weighing. But there must be some better method! It is desirable that the time delay in the filter be as short as possible. I would be most grateful for any help in this, be it with an explanation of how to implement the filter, or a hint as to where I can find this. Manfred Mornhinweg http://ludens.cl
Low pass filter on a PIC
Started by ●October 10, 2008
Reply by ●October 10, 20082008-10-10
Manfred wrote:> Hi all! > > Maybe someone here might help me with a DSP problem that should be > trivial, but to me, as a newbie to DSP, is not. > > I'm developing a microcontroller-based speed controller for my small > hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the > zero crossings of the 50Hz signal coming from the generator, and measuring > the duration of each half cycle, obtaining a signal at a rate of 100 > samples per second. The signal is in 16 bit format, and nominally 10000 > counts tall. Typical variations are a few tens of counts, and occasionally > as much as 1000 counts. I'm subtracting the 10000 reference, and using the > resulting error signal as input to a proportional-integral control > function, whose output drives dump loads that burn off the excess output > from the generator. > > The little problem I have is that a small 420Hz signal on the power line, > apparently coming from the generator's internal voltage regulator, is > causing jitter in the zero crossings, which is in turn causing the > proportional function of my controller to imprint an unwanted modulation on > its output. So I would like to apply a low pass filter function to my > signal, with a cutoff frequency of roughly 20Hz, before the P-I function. > > The question is: How can I implement this DSP low pass filter? It has to > be in some simple way, because neither the PIC nor I can handle overly > complex math! > > The only thing I can come up with is averaging the last several samples, > perhaps with some weighing. But there must be some better method! > > It is desirable that the time delay in the filter be as short as > possible. > > I would be most grateful for any help in this, be it with an explanation > of how to implement the filter, or a hint as to where I can find this. > > Manfred Mornhinweg > http://ludens.clYou want a simple first-order lowpass IIR filter. You can implement it in one line of C: output = gain * (input - output); (note that the 'output' variable here must have a static lifetime). A 20Hz cutoff is pretty high for a sampling rate of 50Hz -- you're not going to get much attenuation of your noise. But you can fiddle around with your gain parameter (lower gain = lower bandwidth) to a compromise between noise reduction and overall loop bandwidth (and hope it's a good one). Keep in mind that as you lower the filter bandwidth you'll have to back off your proportional and integral gains to keep the loop stable. You may also find that you need to use more than 16 bits in the filter to maintain enough precision (see the end of this article http://www.embedded.com/2000/0010/0010feat3.htm for comments on data path widths). You may find some of the other articles on my website interesting: http://www.wescottdesign.com/articles.html. And of course, if you want to get the full meal deal, check out my book. It puts the math back in, but I tried to keep it grounded in reality. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com Do you need to implement control loops in software? "Applied Control Theory for Embedded Systems" gives you just what it says. See details at http://www.wescottdesign.com/actfes/actfes.html
Reply by ●October 10, 20082008-10-10
On Oct 10, 4:19 pm, "Manfred" <mmorn...@gmx.net> wrote:> Hi all! > > Maybe someone here might help me with a DSP problem that should be > trivial, but to me, as a newbie to DSP, is not. > > I'm developing a microcontroller-based speed controller for my small > hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the > zero crossings of the 50Hz signal coming from the generator, and measuring > the duration of each half cycle, obtaining a signal at a rate of 100 > samples per second. The signal is in 16 bit format, and nominally 10000 > counts tall. Typical variations are a few tens of counts, and occasionally > as much as 1000 counts. I'm subtracting the 10000 reference, and using the > resulting error signal as input to a proportional-integral control > function, whose output drives dump loads that burn off the excess output > from the generator. > > The little problem I have is that a small 420Hz signal on the power line, > apparently coming from the generator's internal voltage regulator, is > causing jitter in the zero crossings, which is in turn causing the > proportional function of my controller to imprint an unwanted modulation on > its output. So I would like to apply a low pass filter function to my > signal, with a cutoff frequency of roughly 20Hz, before the P-I function. > > The question is: How can I implement this DSP low pass filter? It has to > be in some simple way, because neither the PIC nor I can handle overly > complex math! > > The only thing I can come up with is averaging the last several samples, > perhaps with some weighing. But there must be some better method! > > It is desirable that the time delay in the filter be as short as > possible. > > I would be most grateful for any help in this, be it with an explanation > of how to implement the filter, or a hint as to where I can find this. > > Manfred Mornhinweghttp://ludens.clYes - it's called a resistor and a capacitor. Put the capacitor to ground and the resistor in series. Voila and no programming - now that's such a great discovery that two components can do better than a pic. Hardy
Reply by ●October 10, 20082008-10-10
"Manfred" <mmornhin@gmx.net> wrote in news:orKdnemiBNUqVHPVnZ2dnUVZ_hudnZ2d@giganews.com:> The only thing I can come up with is averaging the last several samples, > perhaps with some weighing. But there must be some better method!That's pretty much the answer. You can choose ideal weightings according to what your goals are, but a moving average is what things come down to. One caveat is that unless you're a bit careful, using timer interrupts and the like to trigger your sampling, you could have a good deal of jitter in your sample rate, and the math behind the low pass filter can get pretty complicated. You'll still be "low pass", but you'll have problems characterizing the filter. Another caveat is that you'll probably find yourself implementing the filter with integer math, and if you're not used to that, you could get yourself in a bit of a mess. -- Scott Reverse name to reply
Reply by ●October 10, 20082008-10-10
Manfred wrote:> I'm developing a microcontroller-based speed controller for my small > hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the > zero crossings of the 50Hz signal coming from the generator, and measuring > the duration of each half cycle,> The little problem I have is that a small 420Hz signal on the power line, > apparently coming from the generator's internal voltage regulator, is > causing jitter in the zero crossings, > So I would like to apply a low pass filter function to my > signal, with a cutoff frequency of roughly 20Hz, before the P-I function. >The PI function is a lowpass filter by itself. However there is not much you can do when the noise is already in. Add an analog filter upfront the PIC comparator. That will reduce the interferrence without significantly degrading the performance of the loop. Vladimir Vassilevsky DSP and Mixed Signal Design Consultant http://www.abvolt.com
Reply by ●October 10, 20082008-10-10
HardySpicer wrote:> On Oct 10, 4:19 pm, "Manfred" <mmorn...@gmx.net> wrote: >> Hi all! >> >> Maybe someone here might help me with a DSP problem that should be >> trivial, but to me, as a newbie to DSP, is not. >> >> I'm developing a microcontroller-based speed controller for my small >> hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the >> zero crossings of the 50Hz signal coming from the generator, and measuring >> the duration of each half cycle, obtaining a signal at a rate of 100 >> samples per second. The signal is in 16 bit format, and nominally 10000 >> counts tall. Typical variations are a few tens of counts, and occasionally >> as much as 1000 counts. I'm subtracting the 10000 reference, and using the >> resulting error signal as input to a proportional-integral control >> function, whose output drives dump loads that burn off the excess output >> from the generator. >> >> The little problem I have is that a small 420Hz signal on the power line, >> apparently coming from the generator's internal voltage regulator, is >> causing jitter in the zero crossings, which is in turn causing the >> proportional function of my controller to imprint an unwanted modulation on >> its output. So I would like to apply a low pass filter function to my >> signal, with a cutoff frequency of roughly 20Hz, before the P-I function. >> >> The question is: How can I implement this DSP low pass filter? It has to >> be in some simple way, because neither the PIC nor I can handle overly >> complex math! >> >> The only thing I can come up with is averaging the last several samples, >> perhaps with some weighing. But there must be some better method! >> >> It is desirable that the time delay in the filter be as short as >> possible. >> >> I would be most grateful for any help in this, be it with an explanation >> of how to implement the filter, or a hint as to where I can find this. >> >> Manfred Mornhinweghttp://ludens.cl > > Yes - it's called a resistor and a capacitor. Put the capacitor to > ground and the resistor in series. Voila and no programming - now > that's such a great discovery that two components can do better than a > pic.One of us doesn't understand his approach. I think that he counts some high-frequency oscillator during the interval between zero crossings to determine the period of his waveform, the result of each measurement being a number; a bunch of bits. Where should the capacitor go? Jerry -- Engineering is the art of making what you want from things you can get. ����������������������������������������������������������������������� ** Posted from http://www.teranews.com **
Reply by ●October 10, 20082008-10-10
Manfred wrote:> Hi all! > > Maybe someone here might help me with a DSP problem that should be > trivial, but to me, as a newbie to DSP, is not. > > I'm developing a microcontroller-based speed controller for my small > hydroelectric plant. I'm using a PIC 16F628 for this. I'm detecting the > zero crossings of the 50Hz signal coming from the generator, and measuring > the duration of each half cycle, obtaining a signal at a rate of 100 > samples per second. The signal is in 16 bit format, and nominally 10000 > counts tall. Typical variations are a few tens of counts, and occasionally > as much as 1000 counts. I'm subtracting the 10000 reference, and using the > resulting error signal as input to a proportional-integral control > function, whose output drives dump loads that burn off the excess output > from the generator. > > The little problem I have is that a small 420Hz signal on the power line, > apparently coming from the generator's internal voltage regulator, is > causing jitter in the zero crossings, which is in turn causing the > proportional function of my controller to imprint an unwanted modulation on > its output. So I would like to apply a low pass filter function to my > signal, with a cutoff frequency of roughly 20Hz, before the P-I function. > > The question is: How can I implement this DSP low pass filter? It has to > be in some simple way, because neither the PIC nor I can handle overly > complex math! > > The only thing I can come up with is averaging the last several samples, > perhaps with some weighing. But there must be some better method! > > It is desirable that the time delay in the filter be as short as > possible. > > I would be most grateful for any help in this, be it with an explanation > of how to implement the filter, or a hint as to where I can find this.Manfred, A simple low-pass filter might suffice. The conventional symbols used for digital data streams are x[n] for the input data and y[n] for the result, where n is the number of the sample in the sequence. A unity-gain first-order filter is y[n] = k*x[n] + (1-k)*y[n-1]. It is called an exponential averager; k is less than but close to 1. There is usually an adequate choice of k that will allow the multiplications to be accomplished with a shift and a subtraction. I think your problem may be due in part to a (probably unwarranted) assumption that there are no even harmonics in your generated waveform. Even harmonics will cause alternate half cycles to have different periods. You should measure the period of an entire cycle by triggering on positive- or negative-going zero crossings, not both. Jerry -- Engineering is the art of making what you want from things you can get. ����������������������������������������������������������������������� ** Posted from http://www.teranews.com **
Reply by ●October 10, 20082008-10-10
"Manfred" <mmornhin@gmx.net> writes:> [...]Hi Manfred, I have the advantage of seeing the other posters' suggestions and your repsonse, but how about the following: y[n] = x[n] - 0.61803 * x[n-1] + x[n-2] ? This is an FIR notch filter with a notch right at 20 Hz. The 0.61803 term is 2*cos(0.4*pi). -- % Randy Yates % "She has an IQ of 1001, she has a jumpsuit %% Fuquay-Varina, NC % on, and she's also a telephone." %%% 919-577-9882 % %%%% <yates@ieee.org> % 'Yours Truly, 2095', *Time*, ELO http://www.digitalsignallabs.com
Reply by ●October 10, 20082008-10-10
Hi Randy,>I have the advantage of seeing the other posters' suggestions and >your repsonse,For some reason my follow-up ended up posted as a new thread! I don't know if I pressed the wrong button, or if it happened for some other reason. So now we have two threads with this...> but how about the following: > > y[n] = x[n] - 0.61803 * x[n-1] + x[n-2] > >? This is an FIR notch filter with a notch right at 20 Hz. >The 0.61803 term is 2*cos(0.4*pi).But I don't need a notch filter! I need a low pass... Anyway, it's interesting! I will reply to the other responses in the other thread, to try keeping some sort of order. Manfred.
Reply by ●October 10, 20082008-10-10
"Manfred" <mmornhin@gmx.net> writes:> Hi Randy, > >>I have the advantage of seeing the other posters' suggestions and >>your repsonse, > > For some reason my follow-up ended up posted as a new thread! I don't know > if I pressed the wrong button, or if it happened for some other reason. So > now we have two threads with this... > >> but how about the following: >> >> y[n] = x[n] - 0.61803 * x[n-1] + x[n-2] >> >>? This is an FIR notch filter with a notch right at 20 Hz. >>The 0.61803 term is 2*cos(0.4*pi). > > But I don't need a notch filter! I need a low pass... Anyway, it's > interesting!Hi Manfred, I thought what you needed was to get rid of the 20 Hz interference. Is this not true? If it is true, then either one will work, no? In fact, if this is your only goal, the 20 Hz notch should be better. -- % Randy Yates % "With time with what you've learned, %% Fuquay-Varina, NC % they'll kiss the ground you walk %%% 919-577-9882 % upon." %%%% <yates@ieee.org> % '21st Century Man', *Time*, ELO http://www.digitalsignallabs.com






