Hi
I have the formula in my arduino code
X[k] += x[n]* exp((-j*2*pi*(k-1)*(n-1))/N);
What are the headers that will help me perform the exponential function?
Hi
I have the formula in my arduino code
X[k] += x[n]* exp((-j*2*pi*(k-1)*(n-1))/N);
What are the headers that will help me perform the exponential function?
What are the headers that will help me perform the exponential function?
You don't need to include any headers.
Is it not necessary to add in
#include "math.h"
Is it not necessary to add in
You could try without adding that header file faster than you can ask here. If it doesn't compile without the math.h file, then add it.
Hi
I tried a simple exp program and it gave the correct answer without math.h included
Thanks!!!!
Hi
The mathematical formula for DFT functions is
N
X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
n=1
Which I have converted into the Arduino program as
X[k] += (x[n]) * exp (((-j*2*pi*(k-1)*(n-1)))/N);
I do not get a valid answer
What could possible be the problem
What could possible be the problem
One possibility is that you don't understand what you are doing.
The other is that you failed to realize that the formula you used is for complex numbers, and you are using it for real numbers.
Or both.
Hi
I finally got what I wanted, similar to a dft curve
I would love to see how you assigned a value to "j".
j = sqrt(-1); //similar to a dft curve
Which might be why he did not get a "valid answer"
According to Harbison and Steele, you get a domain error if the argument is negative. Which I know, you know jremington, so without a smiley, I am not sure where you are going with this.
Just poking a little fun.
KeithRB:
Which might be why he did not get a "valid answer"According to Harbison and Steele, you get a domain error if the argument is negative. Which I know, you know jremington, so without a smiley, I am not sure where you are going with this.
jremington:
Just poking a little fun.
I ignored the "j" part and printed the answer, I'm glad i got the curve
And we are glad that you are glad!
My codes
/*
N
X(k) = sum x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
n=1
*/
int i, l;
double Fs = 100; //Sampling Frequency
double X[100], x[100], f[100];
int k,n;
double T= 0.01;
int N = 100;
float pi = 3.14;
//float j = sqrt(-1);
int audio0, audio1;
float audioVolt0, audioVolt1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
//Analog Read 0
audio0 = analogRead(A0);
audioVolt0 = audio0 * (5.0 / 1023.0);
//Analog Read 1
audio1 = analogRead(A1);
audioVolt1 = audio1 * (5.0 / 1023.0);
//Input Signal (Sine curve)
for (l=0; l<100; l++) {
x[l] = sin(2*pi*1*l*T);
//Serial.println(x[l]);
//Serial.print(" ");
}
//Serial.println();
//DFT Algorithm
Serial.print(i);//{counter for data
Serial.print(" Index: ");
Serial.println("X(k)-Freq: ");
for (k=0; k<100; k++) {
for (n=0; n<100; n++) {
X[k] += (x[n]) * exp ((-2*pi*(k-1)*(n-1))/N);
}
f[k] = (k/double(N))*(Fs/2); //Frequency interval
//Print DFT Data
Serial.print(f[k]);
Serial.print(" ");
Serial.print(X[k]);
Serial.println(" ");
X[k] = 0;
}
Serial.println();
i++;
delay(1000);
}
My objective is to make the DFT change with respect to the input from A0 (and later on A1).
How do I define it, any idea folks ?
funfrancis:
My objective is to make the DFT change with respect to the input from A0 (and later on A1).
How do I define it, any idea folks ?
Change in what way?
funfrancis:
My codes/*
N
X(k) = sum x(n)exp(-j2pi(k-1)*(n-1)/N), 1 <= k <= N.
n=1
*/
int i, l;
double Fs = 100; //Sampling Frequency
double X[100], x[100], f[100];
int k,n;
double T= 0.01;
int N = 100;
float pi = 3.14;
//float j = sqrt(-1);
int audio0, audio1;
float audioVolt0, audioVolt1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
//Analog Read 0
audio0 = analogRead(A0);
audioVolt0 = audio0 * (5.0 / 1023.0);
//Analog Read 1
audio1 = analogRead(A1);
audioVolt1 = audio1 * (5.0 / 1023.0);
//Input Signal (Sine curve)
for (l=0; l<100; l++) {
x[l] = sin(2pi1lT);
//Serial.println(x[l]);
//Serial.print(" ");
}
//Serial.println();
//DFT Algorithm
Serial.print(i);//{counter for data
Serial.print(" Index: ");
Serial.println("X(k)-Freq: ");
for (k=0; k<100; k++) {
for (n=0; n<100; n++) {
X[k] += (x[n]) * exp ((-2pi(k-1)(n-1))/N);
}
f[k] = (k/double(N))(Fs/2); //Frequency interval
//Print DFT Data
Serial.print(f[k]);
Serial.print(" ");
Serial.print(X[k]);
Serial.println(" ");
X[k] = 0;
}
Serial.println();
i++;
delay(1000);
}
My objective is to make the DFT change with respect to the input from A0 (and later on A1). How do I define it, any idea folks ?
Sorry but all that is just plain wrong. Your implementation is nothing like the Discrete Fourier Transform (DFT) and will give nothing sensible. You can't go into this blind. You need to spend time learning about Fourier analysis and then time understanding the DFT.
If, as you seem to be trying to do, you implement the DFT using the simplistic method your code implies then your code will be very slow and have very poor accuracy. Both speed and accuracy dramatically improve by using the Fast Fourier Transform (FFT) which is nothing more than an efficient algorithm for implementing the DFT. Using Google there are numerous implementations in both C and C++ that you can find.
stowite:
Sorry but all that is just plain wrong. Your implementation is nothing like the Discrete Fourier Transform (DFT) and will give nothing sensible. You can't go into this blind. You need to spend time learning about Fourier analysis and then time understanding the DFT.If, as you seem to be trying to do, you implement the DFT using the simplistic method your code implies then your code will be very slow and have very poor accuracy. Both speed and accuracy dramatically improve by using the Fast Fourier Transform (FFT) which is nothing more than an efficient algorithm for implementing the DFT. Using Google there are numerous implementations in both C and C++ that you can find.
I formulated the code based on a mathematical formula from Matlab.
Upon printing the result, I got it similar to the one I got in Matlab.
I do realize that arduino is not suitable for heavy is suitable for heavy computation.
I worked with a PhD at my last company...brilliant guy, did great things with MATLAB, but I think starting out with a super high language like that ruined any chance of him understanding embedded programming. We ultimately bought the C translator for him, which he used for matrix transformations and biological modeling. The c code wasn't that bad really. It would probably run on an Arduino Uno.
If you're looking to learn how to program it yourself, however, then you should start off with some "hello world" examples before a DFT. Spending an hour to learn what statements and data types are available to you will save you many hours in the long run.
You can find examples of the FFT on the web if you want to see how that's done in an Arduino. Here is an example -> Audio Spectrum Monitor. Is there a reason why you want the DFT instead of the FFT? I would expect most examples to use the FFT because it's more practical, but maybe you could find an instructional example where the DFT was used.
Good luck with your experimenting. If you do end up learning C, some day you'll find it hilarious that you actually had "//float j = sqrt(-1);" in your program.
funfrancis:
I formulated the code based on a mathematical formula from Matlab.
Upon printing the result, I got it similar to the one I got in Matlab.
Sorry but that is just crap. In my first post in this thread I was trying to be kind but your response is just ridiculous. I have been working with Fourier and other signal analysis for more than 50 years. In 1972 my MSc project was concerned with the application of the FFT to spectral analysis. I understand Fourier techniques. I understand DFT. I understand FFT. You obviously do not understand any of this. Until you have an understanding you will continue to generate crap.
Your learning curve will be very very steep but you will have to take the hit if you are to produce anything other than crap.
P.S. I use MATLAB daily.