I am trying to find the FFT of my LSM6DS3 from the code below the sampling rate was set to 833Hz. The accelerometer was placed on a shaker at an amplitude of 3Vpp and frequency of 200Hz. The data was collected from the Arduino serial monitor.
I am having difficulties with the FFT of the data.
Please I have attached the accelerometer and MATLAB code.
I would appreciate any input or suggestions.
#include <Arduino.h>
#include "SparkFunLSM6DS3.h"
#include "Wire.h"
//#include "SPI.h"
//LSM6DS3 myIMU( SPI_MODE, 10 );
LSM6DS3 myIMU(I2C_MODE, 0x6B);
void setup( void ) {
//Over-ride default settings if desired
myIMU.settings.gyroEnabled = 1; //Can be 0 or 1
myIMU.settings.gyroRange = 2000; //Max deg/s. Can be: 125, 245, 500, 1000, 2000
myIMU.settings.gyroSampleRate = 833; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666
myIMU.settings.gyroBandWidth = 200; //Hz. Can be: 50, 100, 200, 400;
myIMU.settings.gyroFifoEnabled = 1; //Set to include gyro in FIFO
myIMU.settings.gyroFifoDecimation = 1; //set 1 for on /1
myIMU.settings.accelEnabled = 1;
myIMU.settings.accelRange = 2; //Max G force readable. Can be: 2, 4, 8, 16
myIMU.settings.accelSampleRate = 833; //Hz. Can be: 13, 26, 52, 104, 208, 416, 833, 1666, 3332, 6664, 13330
myIMU.settings.accelBandWidth = 400; //Hz. Can be: 50, 100, 200, 400;
myIMU.settings.accelFifoEnabled = 1; //Set to include accelerometer in the FIFO
myIMU.settings.accelFifoDecimation = 1; //set 1 for on /1
myIMU.settings.tempEnabled = 1;
//Non-basic mode settings
myIMU.settings.commMode = 1;
//FIFO control settings
myIMU.settings.fifoThreshold = 100; //Can be 0 to 4096 (16 bit bytes)
myIMU.settings.fifoSampleRate = 50; //Hz. Can be: 10, 25, 50, 100, 200, 400, 800, 1600, 3300, 6600
myIMU.settings.fifoModeWord = 6; //FIFO mode.
//FIFO mode. Can be:
// 0 (Bypass mode, FIFO off)
// 1 (Stop when full)
// 3 (Continuous during trigger)
// 4 (Bypass until trigger)
// 6 (Continous mode)
Serial.begin(2000000); // start serial for output
delay(1000); //relax...
Serial.println("Processor came out of reset.\n");
//Call .begin() to configure the IMUs
if( myIMU.begin() != 0 )
{
Serial.println("Problem starting the sensor with CS @ Pin 10.");
}
else
{
Serial.println("Sensor with CS @ Pin 10 started.");
}
Serial.print("Configuring FIFO with no error checking...");
myIMU.fifoBegin();
Serial.print("Done!\n");
Serial.print("Clearing out the FIFO...");
myIMU.fifoClear();
Serial.print("Done!\n");
}
void loop()
{
float temp; //This is to hold read data
uint16_t tempUnsigned;
while( ( myIMU.fifoGetStatus() & 0x8000 ) == 0 ) {}; //Wait for watermark
//Now loop until FIFO is empty. NOTE: As the FIFO is only 8 bits wide,
//the channels must be synchronized to a known position for the data to align
//properly. Emptying the fifo is one way of doing this (this example)
while( ( myIMU.fifoGetStatus() & 0x1000 ) == 0 ) {
//Get all parameters
//Serial.print("\nAccelerometer:\n");
//Serial.print(" X = ");
Serial.print(myIMU.readFloatAccelX(), 2);
Serial.print(",");
Serial.print(myIMU.readFloatAccelY(), 2);
Serial.print(",");
Serial.println(myIMU.readFloatAccelZ(), 2);
delay(10); //Wait for the serial buffer to clear (~50 bytes worth of time @ 57600baud)
}
}
Below is the MATLAB code
clear
data=load('green.txt'); %load the data
fs=833; N=length(data); %sampling rate, number of samples
df=fs/N; %frequency spacing of FFT
%% Compute FFT of each channel
data1=data-mean(data); %subtract mean value from each column
DATA=fft(data1);
%% Plot time domain data
dT=1/fs; %time interval
t=(0:N-1)*dT; %vector of times
figure; subplot(211)
plot(t,data(:,1),'-r',t,data(:,2),'-g',t,data(:,3),'-b')
xlabel('Time (s)'); ylabel('Accel.');
title('Acceleration Data'); grid on
%% Plot frequency domain data
f=(0:N/2)*df; %vector of frequencies, for plotting FFT
subplot(212)
plot(f,abs(DATA(1:N/2+1,1)),'-r',f,abs(DATA(1:N/2+1,2)),'-g',f,abs(DATA(1:N/2+1,3)),'-b')
xlabel('Frequency (Hz)'); ylabel('|Accel.|');
legend('ax','ay','az');
title('FFT of Acceleration Data'); grid on

