FFT - Has anyone ever used it?

Hi, I am hoping to use the FFT node - node-red-contrib-fft

It requires data in a certain format as follows:

{ "data" :  [  
{"accelerometer_x" : 9 , "accelerometer_y":2, "accelerometer_z":2},
{"accelerometer_x" : 7 , "accelerometer_y":2, "accelerometer_z":2},
{"accelerometer_x" : 5 , "accelerometer_y":0, "accelerometer_z":0},
{"accelerometer_x" : 7 , "accelerometer_y":2, "accelerometer_z":2}
]}

I receive MQTT data from a 3-axis accelerometer in this format:

{"axis":"g","x":-0.919,"y":0.209,"z":0.258}

Ths gives a bad data inject error.

Can anyone recommend a course of action to correctly format the data?

Thanks.

Do you mean you are sending in one sample at a time? According to the node docs you have to send it an array of samples.

Thanks Colin.

I can strip out the data and add it to an array but I still need to get it into the specific format required by the FFT block.

Get it into an array and then show us what it looks like (in a debug node). Then we can help you with the last bit.

Feed the samples from MQTT through a Change node set to Move msg.payload.x To msg.payload.accelerometer_x, with similar lines in the Change node for the other two and another line to Delete msg.payload.axis. That should get the individual samples correct. Then you need to collect them into an array, then you can get it to the format required using another Change node to Move msg.payload To msg.payload.data.

1 Like

The data is now in an array (5 samples of x, y and z) and changed to msg.payload.data format.

[-1.901,1.17,-0.352,-1.901,1.17,-0.352,-1.901,1.17,-0.352,-1.901,1.17,-0.352,-1.901,1.17,-0.352]

Thanks for your help.

That doesn't look like the format the node says it needs.

With a few more tweaks it now looks like this:

{"data":[{"accelerometer_x":-0.801,"accelerometer_y":0.527,"accelerometer_z":0.193},{"accelerometer_x":-0.801,"accelerometer_y":0.527,"accelerometer_z":0.193},{"accelerometer_x":-0.801,"accelerometer_y":0.527,"accelerometer_z":0.193},{"accelerometer_x":-0.801,"accelerometer_y":0.527,"accelerometer_z":0.193},{"accelerometer_x":-0.801,"accelerometer_y":0.527,"accelerometer_z":0.193}]}

With a few modifications to the data (float to int) the output of the FFT block looks like this:
{"data":[{"accelerometer_x":1,"accelerometer_y":1,"accelerometer_z":1},{"accelerometer_x":1,"accelerometer_y":1,"accelerometer_z":1},{"accelerometer_x":1,"accelerometer_y":1,"accelerometer_z":1},{"accelerometer_x":1,"accelerometer_y":1,"accelerometer_z":1},{"accelerometer_x":1,"accelerometer_y":1,"accelerometer_z":1}],"fft_0":null,"fft_1":null,"fft_2":null,"fft_3":null,"fft_4":null}

Is it all working now?
Why are you converting the floats to ints?

No, the FFT block is outputting NaN for the FFT values.

I convert the accel data using Math.ceil in a function block.

The FFT block gives an error if the input values are not integers equal to 1 or greater.

Right now I just want to see what the FFT data looks like, I can fix precision etc. later.

Try it feeding in the data in the example in the node's readme. If that works then you know that if you provide good data then the node works.

Just to let you know, node-red-contrib-fft hasn't been updated since August 2016 and the author doesn't seem to be active on GitHub anymore.

Plus there is an open issue in GitHub [RangeError from Example Array](https://github.com/LeanBI/node-red-contrib-fft/issues/1) that is still open.

Personally I'd be looking for another way to do what you want to do.

1 Like

I am pretty certain this FFT finction does not work.

Thanks for all your help.