discrete fourier transform
in
Integration and Hardware
•
1 year ago
dear all,
the project i am working is an interface with a random slidewheel on one half and a punctuation (27 values) on the other half.
now i have to implement a discrete fourier transform to create different bands from raw data coming from an EEG (neurosky) but i have never applied this algorithm.
here i attach the code from the main project and an example of the discrete fourier transform i have been said will work
import neurosky.*;
import org.json.*;
ThinkGearSocket neuroSocket;
int attention=10;
int meditation=10;
//-------------------------------
PrintWriter output;
String imageFolderPath = "C:/Users/FerranGaldon/Desktop/final projects MA/- PROCESSING/_10_self_assesment_click_hard_code/data";
File[] files;
PImage[] images;
//-------------------------------
int counter; // Automatically initialized at 0
final int DISPLAY_TIME = 5000; // 15000 ms = 15 seconds
int lastTime; // When the current image was first displayed
//--------------------------------
int numCircles = 27;
int [][] circles; // use a two-dimensional array
//--------------------------------
PImage b;
int value = 150;
//--------------------------------
void setup () {
// Create a new file in the sketch directory
output = createWriter("positions.txt");
size(1024, 350);
//smooth();
//noStroke();
ThinkGearSocket neuroSocket = new ThinkGearSocket(this);
try {
neuroSocket.start();
}
catch (ConnectException e) {
//println("Is ThinkGear running??");
}
smooth();
circles = new int [numCircles][3]; // define the array, every circle needs three parameters (xPos, yPos, circleDiameter)
// fill array only once
for (int i=0; i<numCircles; i++) {
int circleDiameter = int(23);
int xPos = int(109); // keep distance from border
int yPos = int(67); // keep distance from border
circles[i][0]= xPos + (i % 9) * 38; //horizontal position
circles[i][1]= yPos + (i / 9) * 135;//vertical position
circles[i][2]= circleDiameter;
}
//Random Image array upload and timer
File imageFolder = new File(imageFolderPath);
files = imageFolder.listFiles(new FileFilter()
{
public boolean accept(File file)
{
if (file.isDirectory())
return false; // Only files. A directory can have a dot in its name...
String name = file.getName();
return name.endsWith(".png");// in case you want to extend > name.endsWith(".jpg") || name.endsWith(".png") || name.endsWith(".gif");
}
});
images = new PImage[files.length];
for (int i = 0; i < files.length; i++)
{
images[i] = loadImage(files[i].getAbsolutePath());
}
randomizeImages();
showImageInfo();
lastTime = millis();
}
void draw() {
// Images time display
background(255);
fill(#005599);
text(frameCount, 10, 20); // Shows the sketch isn't stopped between each image
if (millis() - lastTime >= DISPLAY_TIME) // Time to display next image
{
counter++;
if (counter == files.length) // Or IMAGE_NB
{
// Displayed all images, re-randomize the images
randomizeImages();
}
lastTime = millis();
showImageInfo();
}
image(images[counter], 552, 0, width - 582, height);
b = loadImage("manikin.jpg");
image(b, 0, 0, width/2, height);
for (int i=0; i<numCircles; i++) {
fill(value);
stroke(20);
ellipse(circles[i][0], circles[i][1], circles[i][2],circles[i][2]);
}
}
void poorSignalEvent(int sig) {
println("SignalEvent "+sig);
}
public void attentionEvent(int attentionLevel) {
println("Attention Level: " + attentionLevel);
attention = attentionLevel;
}
void meditationEvent(int meditationLevel) {
println("Meditation Level: " + meditationLevel);
meditation = meditationLevel;
}
void blinkEvent(int blinkStrength) {
println("blinkStrength: " + blinkStrength);
}
public void eegEvent(int delta, int theta, int low_alpha, int high_alpha, int low_beta, int high_beta, int low_gamma, int mid_gamma) {
println("delta Level: " + delta);
println("theta Level: " + theta);
println("low_alpha Level: " + low_alpha);
println("high_alpha Level: " + high_alpha);
println("low_beta Level: " + low_beta);
println("high_beta Level: " + high_beta);
println("low_gamma Level: " + low_gamma);
println("mid_gamma Level: " + mid_gamma);
output.println("Image selected at: "+day()+"/"+month()+"/"+year()+" - "+hour()+":"+minute()+":"+second()+":"+millis()+ " >>> " + "Attention:" + attention + " / " + "Meditation:" + meditation + " / " + "Delta (1-3Hz):" + delta + " / " + "Theta (4-7Hz):" + theta + " / " + "Low Alpha (8-9Hz):" + low_alpha + " / " + "High Alpha (10-12Hz):" + high_alpha + " / " + "Low Beta (13-17Hz):" + low_beta + " / " + "High Beta (18-30Hz):" + high_beta + " / " + "Low Gamma (31-40Hz):" + low_gamma + " / " + "High Gamma (41-50Hz):" + mid_gamma);
}
void rawEvent(int[] raw) {
//println("rawEvent Level: " + raw);
}
void stop() {
neuroSocket.stop();
super.stop();
}
void mousePressed(){
//row 1
if (mouseX > 100 && mouseX < 120){
if (mouseY > 50 && mouseY < 80){
println("box 1");
output.println("box 1");
}
if (mouseY > 190 && mouseY < 210){
println("box 10");
output.println("box 10");
}
if (mouseY > 330 && mouseY < 350){
println("box 19");
output.println("box 19");
}
}
//row 2
if (mouseX > 138 && mouseX < 158){
if (mouseY > 50 && mouseY < 80){
println("box 2");
output.println("box 2");
}
if (mouseY > 190 && mouseY < 210){
println("box 11");
output.println("box 11");
}
if (mouseY > 330 && mouseY < 350){
println("box 20");
output.println("box 20");
}
}
//row 3
if (mouseX > 180 && mouseX < 200){
if (mouseY > 50 && mouseY < 80){
println("box 3");
output.println("box 3");
}
if (mouseY > 190 && mouseY < 210){
println("box 12");
output.println("box 12");
}
if (mouseY > 330 && mouseY < 350){
println("box 21");
output.println("box 21");
}
}
//row 4
if (mouseX > 210 && mouseX < 230){
if (mouseY > 50 && mouseY < 80){
println("box 4");
output.println("box 4");
}
if (mouseY > 190 && mouseY < 210){
println("box 13");
output.println("box 13");
}
if (mouseY > 330 && mouseY < 350){
println("box 22");
output.println("box 22");
}
}
//row 5
if (mouseX > 250 && mouseX < 270){
if (mouseY > 50 && mouseY < 80){
println("box 5");
output.println("box 5");
}
if (mouseY > 190 && mouseY < 210){
println("box 14");
output.println("box 14");
}
if (mouseY > 330 && mouseY < 350){
println("box 23");
output.println("box 23");
}
}
//row 6
if (mouseX > 290 && mouseX < 310){
if (mouseY > 50 && mouseY < 80){
println("box 6");
output.println("box 6");
}
if (mouseY > 190 && mouseY < 210){
println("box 15");
output.println("box 15");
}
if (mouseY > 330 && mouseY < 350){
println("box 24");
output.println("box 24");
}
}
//row 7
if (mouseX > 330 && mouseX < 350){
if (mouseY > 50 && mouseY < 80){
println("box 7");
output.println("box 7");
}
if (mouseY > 190 && mouseY < 210){
println("box 16");
output.println("box 16");
}
if (mouseY > 330 && mouseY < 350){
println("box 25");
output.println("box 25");
}
}
//row 8
if (mouseX > 370 && mouseX < 390){
if (mouseY > 50 && mouseY < 80){
println("box 8");
output.println("box 8");
}
if (mouseY > 190 && mouseY < 210){
println("box 17");
output.println("box 17");
}
if (mouseY > 330 && mouseY < 350){
println("box 26");
output.println("box 26");
}
}
//row 9
if (mouseX > 400 && mouseX < 420){
if (mouseY > 50 && mouseY < 80){
println("box 9");
output.println("box 9");
}
if (mouseY > 190 && mouseY < 210){
println("box 18");
output.println("box 18");
}
if (mouseY > 330 && mouseY < 350){
println("box 27");
output.println("box 27");
}
}
output.flush(); // Writes the remaining data to the file
}
void randomizeImages()
{
for (int i = images.length - 1; i > 0; i--)
{
int j = int(random(i + 1));
// Swap
PImage ti = images[i];
images[i] = images[j];
images[j] = ti;
// Swap
File tf = files[i];
files[i] = files[j];
files[j] = tf;
}
}
void showImageInfo()
{
File tf = files[counter];
output.println("-----------------------");
output.println("Name: " + tf.getName());
output.println("-----------------------");
}
void exit(){
output.flush(); // Writes the remaining data to the file
output.close(); // Finishes the file
super.exit();
}
//-------------------------- discrete fourier transform example
//do the DFT for each value of x sub j and store as f sub j
double f[] = new double[n/2];
for (int j = 0; j < n/2; j++) {
double firstSummation = 0;
double secondSummation = 0;
for (int k = 0; k < n; k++) {
double twoPInjk = ((2 * Math.PI) / n) * (j * k);
firstSummation += x[k] * Math.cos(twoPInjk);
secondSummation += x[k] * Math.sin(twoPInjk);
}
f[j] = Math.abs( Math.sqrt(Math.pow(firstSummation,2) +
Math.pow(secondSummation,2)) );
double amplitude = 2 * f[j]/n;
double frequency = j * h / T * sample_rate;
System.out.println("frequency = "+frequency+", amp = "+amplitude);
}
1