Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Software based Second order Low pass filter and FFT

Status
Not open for further replies.

wogoos

Member level 2
Joined
Apr 20, 2010
Messages
49
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,909
Dear reader
Does any one have an algorithm that functions as a second order Low Pass Filter LPF which specifies the parameters in terms of Cutoff frequencies and phase shifts?.
Thanks
===============================

I ask this question for the following reason, commends are welcome if I make a mistake
I want to analyses a line voltage and line current with FFT by sampling them at the same time in two FFT sample arrays.
I have done some experiments with FFT in excel en saw interesting phenomenons which are

  1. FFT Bin one generates amplitude and phase of a zero harmonic sinusoidal under the condition that the FFT Bin frequency spacing is close to or exactly the sinusoidal frequency of the signal.
  2. If the signal is not zero harmonic sinusoidal but constant the other bins generate the harmonics which allow to calculate the THD of the signal.
  3. By measuring the current and voltage at the same time, also the phase difference between the two is calculated from the difference between the FFT signals.
  4. For FFT no peak detection or other synchronizations mechanisms are needed to calculate the peak current and voltage phase and THD. The only thing needed is a accurate frequency of the first harmonic (FFT bin one)) and sampling both signal at the same time
Consider the frequency of the first Bin/harmonic unknown but between 40 ... 70 Hz. By starting the sample rate corresponding with the 40Hz FFT bin we can scan the 40..70Hz band for a maximum amplitude signal which ones found corresponds with the net frequency of the signal we measure. Scanning is done by adjusting the timer interrupt of a 16bit timer of a controller which runs on 16Mhz with a timer prescaler equal to 1. This timer interrupt service routine reads the voltage and current signals and the scan resolution must be high around the frequency of the actual line monitored.

Assuming this method is correct(i think i am) we can 1) measure accurate the frequency of a net signal and following 2) compute the FFT and the corresponding amplitudes, phase and THD of a signal without the need for peak detectors and or synchronization mechanisms.

To do a scan of the input between 40..70 Hz preferably is done on a clean sinusoidal without harmonics. For that reason I need a second order filter. Preferably is done means that its nice to have but not a must.

i hope some can help me with a filter algorithm and confirm whether or not i'm correct with my description.

Thanks Oscar Goos
 

Are you referring to an LC low pass filter?

Choose L so it passes almost entire current at cutoff frequency to your load. Its choke action starts to take effect at a frequency 1.5 to 2 times that of cutoff frequency.

Choose C to create combined LC resonant frequency 1.5 to 2 times your cutoff frequency. At the same time C shall correct for power factor error at the cutoff frequency.

The L:C ratio is high for small current, low for large current.
 

Are you referring to an LC low pass filter?

Choose L so it passes almost entire current at cutoff frequency to your load. Its choke action starts to take effect at a frequency 1.5 to 2 times that of cutoff frequency.

Choose C to create combined LC resonant frequency 1.5 to 2 times your cutoff frequency. At the same time C shall correct for power factor error at the cutoff frequency.

The L:C ratio is high for small current, low for large current.
Hi Brad
no I referring to a second order filter implemented in software. I'm sampling an analog input connected to a net line of 60Hz which might contain harmonics.
Hope its more clear now
Regards
 

Hi,

The informations are a bit vague.
And I didn´t get what you want to achieve.
Maybe a sketch could clarify.

****
Filter:
* you urgently need an analog anti-aliasing filter in front of the ADC.
* but any other filter (analog or digital) wont bring much improvement when you run an FFT.

****
The first bin (bin 0) contains the DC (average) value of all input samples. Because you don´t have an "imaginary input window" you will see only real values.

FFT frequency step size: = 1/(measurement_window_size_ in_seconds)
Example: if your window is 100ms in width then the step size = 1/10ms = 10Hz
Thus: bin0 = 0Hz (DC), bin1 = 10Hz, bin2 = 20Hz ... and so on.

Mind:
* that each bin has a bandwidth (+/-5Hz in the example. This means you won´t (easily) find out frequencies like 10.3Hz, 11Hz, 15Hz and all other non_integer multiples of 10Hz)
* that if your input signal contains frequencies other than DC and integer multiples of step_size (10Hz in example) you will see an increased noise floor. Sometimes they look like "overtones". This may cause error in the THD measurement.
You may use a windowing function (like Hanning) to reduce the noise floor, but then the values of the frequency bins ar not that exact (not steep, not fcussed) than without the windowing function.

Klaus
 

Hi Klaus,
I can see that you know your stuff. On the right a picture of what I have in mind (click on it). Digital power meter algorithm.jpg

You did bring up and important point "you urgently need an analog anti-aliasing filter in front of the ADC." I do have that but it has a cut off frequency of 1000Hz. In my explanation I forgot that aspect.
I had the following idea and I hope this clears things up. I want to scan a line frequency between 40..70 Hz starting with a 40*64=2564Hz ( sample frequency (fs), Sequentially filter these sample results in a digital filter that I haven't got yet and do a FFT to see if I have a peak value at my bin1 (40Hz). If so the line frequency is that value. If not scan for the next frequency with a step of 0.1 Herz so that is 40.1Hz with a scan frequency of 40.1*64=2570.4 Hz and see if you find a peak there. If not and so on till you find a peak value in bin1=40.1Hz...40.2......60Hz.........70hz..

My Aliasing filter has a second order fc of 1000hz and my minimum fs is 2564. That is more then 2xfc=2x1000Hz and Nyquist told me that this should work :smile:. I need to get rid of possible harmonics in the input signal because they may show up in the FFT Bins when I scan the line. To get rid of possible harmonics in the scanned input signal I do a second digital filtering of the input signal with a filter i'm asking for.( i realize while I write this comment that this second filtering might not be needed because I focus on the first harmonic in Bin1 who cares what happens in the other bins.)

You are right the FFT results around or close to a Bin frequency in the input signal don't show up in the Bins. I saw that too but they do have a bandwidth. So with a small scan step you can detect the existence of a nearby frequency in a bin by looking at its amplitude, Around that frequency value I want to do a fine scan with a fine tuning of fs. In this way I can measure and calculate the frequency of an input signal. It is similar to what happens in a radio I guess.
Does this approach has change of success you think.
 

Hi,

there´s a problem.
a digital filter doesn´t work like anlog filters. Analog filters have a fixed cutoff frequency dependin on the part values.
But a digital filter depends on it´s parameters ... and it always depends on sampling frequency.
This may cause problems here, because you change the sampling frequency.

This also has influence on the tap frequencies of the FFT. They all depend on sampling frequency.

As already said: I see no benefit in the digital filter before the FFT. What´s your idea behind it?
later in txt you write:
( i realize while I write this comment that this second filtering might not be needed because I focus on the first harmonic in Bin1 who cares what happens in the other bins.)
Yes, that´s the point. This makes the filter meaningless.


Sequentially filter these sample results in a digital filter that I haven't got yet and do a FFT to see if I have a peak value at my bin1 (40Hz). If so the line frequency is that value.
As already said: the FFT output has a bandwidth of (about?) the FFT_frequency_step size. This means the "40Hz" tap will show frequencies from 20Hz ... 60Hz. But it has "fading" character.
If there is a 45Hz signal, then the 40Hz tap will show the highest peak, but the 80Hz tap will also show some signal. At 60Hz bout taps (40Hz and 80Hz) will show about the same amplitude.

My Aliasing filter has a second order fc of 1000hz and my minimum fs is 2564. That is more then 2xfc=2x1000Hz and Nyquist told me that this should work .
Sadly not perfect.
Let´s say you have a 2nd order 1000Hz butterworth LPF as anti aliasing filter.. and fs = 2564Hz.
Then the nyquist limit is fs/2 =1282Hz.
At this frequency the attenuation of your anti-aliasing filter is about 6dB. The signal is still there. But just with half of the amplitide.
Let´s say there is an input of 2000Hz with 1V RMS amplitude. The LPF will attenuate it to about -11dB this is about 280mV RMS. And this surely will cause an alias frequency.
2000Hz is mirrored at 1282Hz .. resulting in 564Hz with 280mV RMS.

I hope I don´t discourage you with that news... Go on with your idea.

Klaus
 

If each scan is simply looking for one possible frequency then you're doing a DFT at one frequency which is also known as sin correlation. Doing this as you describe, with an integer number of samples per cycle of the measured frequency is an ideal and very accurate way of doing what you want - but slow. Perhaps you could do an initial FFT scan to zero in on the area of interest and sweep that range more accurately with sin correlation.

Also what about faster sampling rates to increase your resolution. 2k samples/s is slow.

As Klauss already mentioned, the value of digital filtering prior to an FFT is limited or worthless. Fourier analysis is very powerful and is already a form of filter.

However the answer to your original question is to look up IIR filters. A 'biquad IIR' is a very common structure creating two poles and two zeros. Two first order IIR's cascaded can also give you two poles of low pass filtering.
 

Hi,

there´s a problem.
a digital filter doesn´t work like anlog filters. Analog filters have a fixed cutoff frequency dependin on the part values.
But a digital filter depends on it´s parameters ... and it always depends on sampling frequency.
This may cause problems here, because you change the sampling frequency.

T depend on his also has influence on the tap frequencies of the FFT. They allsampling frequency.

As already said: I see no benefit in the digital filter before the FFT. What´s your idea behind it?
later in txt you write:
Yes, that´s the point. This makes the filter meaningless.



As already said: the FFT output has a bandwidth of (about?) the FFT_frequency_step size. This means the "40Hz" tap will show frequencies from 20Hz ... 60Hz. But it has "fading" character.
If there is a 45Hz signal, then the 40Hz tap will show the highest peak, but the 80Hz tap will also show some signal. At 60Hz bout taps (40Hz and 80Hz) will show about the same amplitude.


Sadly not perfect.
Let´s say you have a 2nd order 1000Hz butterworth LPF as anti aliasing filter.. and fs = 2564Hz.
Then the nyquist limit is fs/2 =1282Hz.
At this frequency the attenuation of your anti-aliasing filter is about 6dB. The signal is still there. But just with half of the amplitide.
Let´s say there is an input of 2000Hz with 1V RMS amplitude. The LPF will attenuate it to about -11dB this is about 280mV RMS. And this surely will cause an alias frequency.
2000Hz is mirrored at 1282Hz .. resulting in 564Hz with 280mV RMS.

I hope I don´t discourage you with that news... Go on with your idea.

Klaus

Hi Klaus,
1) About the filter parameters, correct these are fs dependence and need to be calculated per fs. The picture doesn't show that and I have added it.

2) The idea I have to remove the higher harmonics by filtering the input signal comes from the fact that an FFT produces in the first Bin1 a signals peak value in case the first bin frequency is equal to the signal frequency. In the case Bin frequency = signal frequency all others Bin frequencies are theoretical zero, I do see that they can have very small values much smaller than one. In the case the Bin frequency != signal frequency the FFT produces a spectrum around Bin1. you are correct on that. That spectrum is for me an indication that my Bin frequency is not equal to the signal frequency. Based on that I can start scanning by changing step by step the Bin1 Frequency till that one is the only bin with a value. Ones that is the case the Bin frequency is the frequency of the signal.

3) about your explanation dealing with a 45Hz signal and a 40Hz Bin1 frequency. I have tested in Exel a 60 Hz signal that is sampled with a 44Hz Bin1 frequency. You are right that produces various fading harmonics in a multiple of 44 with a peak around the 44..88hz. As soon as I scan with Bin frequency of 60 I see only Bin with one result. Adjusting the Bin1 frequency from 44 ...60 in steps shows that the Fading harmonics step by step disappear.

4) Your point of the second order anti-aliasing filter is correct that higher frequency signal is there and might show up as a mirror in the spectrum. I will have look if I can increase the sampling frequency
but also y sampling buffer because the quotient of the two (fs/N = Bin1 frequency) determine the Bin frequency which I have to adjust to the signal frequency.

5) Don't worry about discourage i'm not a snowflake :thinker: You helped me a lot thanks for that

- - - Updated - - -

Hi ASDF44,
Thanks for responding.
1) your suggestion of increasing the sampling rate would require me also to increase numbers of samples because I want the FFT Bins bandwidth exactly to be the frequency of the sampled signal. how ever i will keep it in mind.
2) IIR's. i found an IIR filter functions which can be used in LPF, BPF, HPF mode. It is according the following functions. yn=b0*Xn + b1*xn-1 + b2*xn-2 - a1*yn-1 - a2yn-2
3) Based on the discussion i had with Klaus I'm not sure if I will use it. Added a figure that shows what my ideas are to implement in case I want and need it.

The filter is not filtering a 64 sample array bin in to a filtered 64 array bin, it filters per sample. It uses for this a 2x3 Array. This solves the problem of starting this filter because the first two Y-1 and Y-2 are unknown at the start of this filter and it takes time to get rid of this startup effect. That is why I suggest to let the filter do its work for some time say 10 ms and then do the sampling from Y0 being the filtered signal.
I hope that its clear to you this stuff is not easy to understand and I hope you can follow it.
Below schematic picture of the idea I have
Digital power meter algorithm.jpg
 

Hi,

Regarding sample frequency and anti aliasing filter:
* consider to use a delta-sigma-ADC. They are cheap, come with high resolution and because their oversampling technique with digital filters the analog anti-aliasing-filter is simple.

Klaus
 

Hi Klaus
Yep that a possibility but I was thinking on this and maybe the peaces of the puzzle fall together now.
Minimum external hardware and all the logic in software. This schematic galvanic separates the line from the electronics for current and Voltage and creates two signals a 0..5 volt voltage or current reading and a zero crossing detector. Initially I wanted to use the zero crossing for frequency detection but I decided to investigate how to do it with FFT. With this circuit no specific interfaces are needed only an analog in signal and if needed a digital for the zero crossing. Further more no separated galvanic power supply's are needed or worse galvanic IO devices. All fits in a 37mm din rail case. What is needed some G$%^dam complex code but I think that's possible. My plan is to use a ESP32 to do the logic and build a web based UI interface.
Further commends are welcome and thanks for being critical and helping me.
The schematics and layout are shown below
Voltage Current sensor.jpg
Power interface.jpg
 

Hi,

Honestly, I don´t understand the circuit. Why the "double wiring"? Why the connection to VCC and GND?

It seems you use VCC/2 as bias voltage:
If you want to do ADConversions with some quality I don´t recommend to use VCC as reference. Neiter for the bias voltage nor for the ADC_VRef. Use a true reference voltage instead.

You talk about overtones:
Please take care that the transformer does not introduce overtones.

and you talk about simultaneous measurement of voltage and current:
Please tkae care that the transformer does not introduce unexpected phase shift.

To get high precision I usually don´t use isolation at the analog signals. If necessary I use isolation at the digital side - after the ADC.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top