Using FFT_test.fcfx

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 6.

Moderator: Benj

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Thanks for letting us know about the problem your facing. I will investigate and see if I can work out what's going wrong.

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times

Re: Using FFT_test.fcfx

Post by kersing »

Hi Larry,

What kind of wave form is you input?

Best regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Jac
Wave form is sine wave from a function generator. I tried 0 center and also 0 at the bottom on the sine wave with the same results in both cases. I also checked the input sine wave on a scope.
500 hz.jpg
500 hz.jpg (26.37 KiB) Viewed 18017 times
Larry

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

I just noticed in the console that the Input buffer contains 32 pieces of data which is correct for the buffer size allocated, however after the FFT is processed the console shows 192 pieces of data for FFT_Real buffer and the same for FFT_imag buffer which also has 32 spaces allocated for each. This does not seem right to me. Perhaps, this points to the problem.

Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hello,

I seem to be getting similar results so will continue this afternoon and see if I can find the source of the problem.
20150609_150331.jpg
20150609_150331.jpg (77.95 KiB) Viewed 18004 times
The console outputs any value written to the buffer, the FFT performs several passes as it re-orders the data and all of the passes will be shown on the console. Hence the extra values you are seeing.

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
Thank you. A solution would be awesome.
Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

A solution would indeed be awesome. So far it is eluding me but I believe some progress has been made.
FFT2.png
(1.24 MiB) Downloaded 6900 times
Red = Signal In
Green = FFT Output Real
Blue = FFT Output Imaginary
Black = FFT Output Frequency Banks
DSPFFT.fcpx
(21.17 KiB) Downloaded 419 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hello,

I've spent most of the morning working on this issue and have made progress but don't seem to really be getting anywhere fast. So I will do some of my scheduled work and then jump back on this when I get some more free time.

Sorry the news couldn't be better. Is this for a commercial project and do you have a deadline you are working to?

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
Yes commercial project.
I don't have a hard deadline but I am being pushed.
Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Ok understood. I'm fairly certain the code is right but there is an overflow issue or something similar going on. I will do some more reading and check my maths is making sense.

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben

Thanks for the update. Eagerly awaiting a solution.

By the way, this is not a chip specific problem I hope, as I intend to migrate to 30F3012 (18 pin) as the final chip. I'm presently using the 4013 because of the extra peripherals to aid in the diagnostics as the program evolves.

Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hello Larry,

No it's not chip specific, it's a problem with the maths.

Progress...

I have started again in a blank Flowcode project and have now managed to get somewhere.

Here is a file which shows how I got it working. Depending on your application this file may be enough to allow you to continue.
FFT_Rehash.fcfx
(25.63 KiB) Downloaded 379 times
I've not tried it on hardware yet but I'm hopeful.

Tomorrow I will take this code and place it back into the DSP component so hopefully this can function correctly too.

kersing
Valued Contributor
Valued Contributor
Posts: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times

Re: Using FFT_test.fcfx

Post by kersing »

Gents,

Please keep in mind you might get artefacts if the sample does not start and end at a zero crossing of the input sine. Using a window function (Hanning, Hamming or any of the others) will reduce that effect, but not eliminate it. Also I found I had to reduce the amplitude of samples by half to prevent overflow while working with FFT. (I'm using dsPIC FFT functions as used in my article wrapped in a FC6 component)

Best regards,

Jac
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hello,

I've managed to make some progress and route out a few bugs.
DSPFFT.fcpx
FFT Component - Fixed FFT Maths Routines
(15.62 KiB) Downloaded 280 times
DSPFreqGen.fcpx
Freq Gen - Fixed a problem with 16-bit data being stored in 8-bit locations
(32.89 KiB) Downloaded 351 times
lut.fcpx
LUT - Fixed a problem with negative numbers
(5.95 KiB) Downloaded 297 times
Here is my test program.
DSPFFT.fcfx
FFT Test Program 30F4013
(28.79 KiB) Downloaded 308 times
This is working great in simulation and on hardware. I've just tried with some real world data and this also seems ok though still not perfect. This could be down to what Jac also noticed. I will see if we can add a simple windowing filter on the data before we run the FFT calculations to see if this improves things.

Let me know how your getting on.

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben

I played with it and still don't get useful output data from the bins. My program will be making decisions based on certain bin content and will not be reconstructing any of the output as it has no use after the bin data evaluation.

I noticed that your clock is set at 96Mhz and timer-1 set at 16khz and FFT sampling at 8khz. Should those numbers be significant to me, or is that just where you left them for your testing purposes?

Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hi Larry,

I have now added a filter property to automatically do a window filter on the data and this is now giving me a very nice frequency spike.
DSPFFT.fcpx
(14.02 KiB) Downloaded 359 times

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
Will do more tests later today
Larry

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
Seems to be very much improved. I am getting some usable results.Is it now complete, or is there more tweaking to do?
I will be doing more experiments today.
Larry

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
It seems that discrimination between frequencies is poor. For instance, I set up to read the 500hz bin and get a a level of 120 from that bin with 500hz signal applied, then I take the freq on the function generator up to higher frequencies in 100hz steps up to 1khz and still get 120 and higher from the 500hz bin. I would expect that as I move away from the frequency designated for that bin, that the number in the bin would diminish substantially.
I am using a sample rate of 2000 with sample count of 32 so I get 16 bins 63hz per bin.
Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hi Larry,

Can you attach your program or a simplified version which exhibits the problem and I will investigate for you.

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben

Here is the latest version of my program, as it seems to change minute by minute. Presently, I am getting values in the #9 bin at frequencies 250hz, 650hz and 750hz. These are the frequencies I input from a function generator with clean sine wave as shown in the photo of my scope in a previous post several above this one.
I am sampling at a rate of 2000 for 32 samples, so that I get 16 bins of final FFT data. Input is 1volt p-p with 0 center.

Larry
FFT_3012_LED.fcfx
(21.28 KiB) Downloaded 322 times
Update: As I was writing this post, I tested with function generator set to 0 at the bottom of the sine wave and was able to get better discrimination.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hi Larry,

Aha yes that might explain things. The ADC input has to be positive, negative voltages will just be read by the micro as 0's. You then subtract an offset from the value in software to get the signal centred around the 0 position. The closer you can get this offset the better frequency discrimination you should achieve.

For example input voltage = 5V peak to peak sinewave, centred around 2.5V

The ADC reads the value as 10-bit Int to give the value 0-1023.

Subtracting 512 from the ADC input will give you -512-511 with the mid point at 0.

...

If the input voltage is instead 2V peak to peak sinewave, centred around 1V

The ADC reads the value as 10-bit Int to give the value 0-409.

Subtracting 205 from the ADC input will give you -205-204 with the mid point at 0.



You could do automatic level detection in your program by using the DSP level component and collecting the max and min values from the input buffer before subtracting the offset and running the FFT.

Offset = (Max - Min) / 2
Signal = Signal - (Offset + Min)

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Here is an example with my attempt at auto levelling.
DSPFFT3.fcfx
(26.45 KiB) Downloaded 318 times
It doesn't work as well as I was hoping. Could be that auto levelling is a bad idea and instead you need to get the levels from the average of several buffers to get a true idea of the actual min and max.

Luckily the DSP Level component has a property to allow you to do just that :D

Upping the decay to 10 does seem to be better but still not as good as simply subtracting the fixed offset.

A decay of 25 seems better again.

SHORTCIRCUIT
Posts: 155
Joined: Thu Feb 10, 2011 4:39 am
Location: Las Vegas, Nevada USA
Has thanked: 40 times
Been thanked: 19 times

Re: Using FFT_test.fcfx

Post by SHORTCIRCUIT »

Hi Ben
Thank you for the response.
I believe the adc on the 30F3012 is 12bit, so full scale 4095. If this is correct I can adjust what you said above to this value. Please comment.
Larry

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using FFT_test.fcfx

Post by Benj »

Hi Larry,

Yep 12-bit ADC = 0-4095


For example input voltage = 5V peak to peak sinewave, centred around 2.5V

Subtracting 2048 from the ADC input will give you -2048-2047 with the mid point at 0.

...

If the input voltage is instead 2V peak to peak sinewave, centred around 1V

The ADC reads the value as 12-bit Int to give the value 0-1638.

Subtracting 819 from the ADC input will give you -819-819 with the mid point at 0.

Post Reply