Doing DFT without using FFT function

236 views (last 30 days)
Hi all,
I'm working on a project that handles ECG data from arduino and ran into some problems while computing the discrete fourier transform of the ECG. I would like to view the transforms and data collection in real time. While the real time data collection works fine, I would prefer not to use the fft function because for academic uses, the hard coded formula of the fourier transform has more learning value. The code in entirety is as shown below:
%initialize communication with arduino
arduino = serial('COM6');
%initialize figures
%fig1 = figure;
%fig2 = figure;
%fig3 = figure;
%fig4 = figure;
%initialize sample and counter
sampleSize = 500;
while (a < sampleSize) %doubles as a counter, 1000 samples takes about 10seconds
%collects raw data in string format
timeRaw = fgets(arduino);
ecgRaw = fgets(arduino);
%converts string data to numbers
time = str2double(timeRaw) / 1000;
ecg = str2double(ecgRaw);
%data allocation
dataTime(a,1) = time;
dataECG(a,1) = ecg;
frequency = 1/time;
fftEcg = fft(ecg);
for k = 1:a
X(k,1) = 0;
for n = 1:a
X(k,1) = X(k,1)+(dataECG(n,1)*exp((-1j)*2*pi*(n-1)*(k-1)/a));
mag(a,1) = abs(X(a,1));
dataFreq(a,1) = frequency;
dataFft(a,1) = fftEcg;
%low pass filter, set at 50Hz
%sampling freq 80~90Hz, partly determined by delay in arduino
if (frequency < 10)
Y = fftEcg;
dataY(a,1) = Y;
Y = 0;
dataY(a,1) = Y;
%inverse FFT
dataIfft(a,1) = ifft(Y);
%plotting of figures, subplots are too small, 4 windows too messy
xlabel('Time (sec)');
ylabel('x(t) magnitude');
xlabel('Frequency (Hz)');
ylabel('X(jw) magnitude');
xlabel('Frequency (Hz)');
ylabel('X(jw) magnitude');
%xlabel('Frequency (Hz)');
%ylabel('Filtered H(jw) magnitude');
xlabel('Frequency (Hz)');
ylabel('Filtered x(t) magnitude');
%command to draw and take in next sample by increasing counter
a = a + 1;
%data collection into csv format
%intialize array for final data set
%finalData = zeros(sampleSize,2);
%data collection for raw data only
%for i=1:(sampleSize-1)
% finalData(a,1) = dataTime(a,1);
% finalData(a,2) = dataECG(a,1);
%csvwrite('data.csv', finalData);
In particular the formula that I keyed in is found in this few lines of code:
for k = 1:a
X(k,1) = 0;
for n = 1:a
X(k,1) = X(k,1)+(dataECG(n,1)*exp((-1j)*2*pi*(n-1)*(k-1)/a));
mag(a,1) = abs(X(a,1));
The results between the fft function and the formula I've input are very different, any ideas how I can modify the formula?
Angus Keatinge
Angus Keatinge on 28 Apr 2018
Edited: Angus Keatinge on 28 Apr 2018
This is wrong, the dft is from 0 to N-1 whereas linspace includes the extremities. You won't only have a redundant value at the last index, but every frequency term will be scaled differently to the N point dft (they will be scaled to an N-1 point dft). It is quite a common error, you can correct it by changing the lines:
w = 2*pi*linspace(0,1-1/len,len);
Xk = exp(-1j*w'*(n-1))*xn';

Sign in to comment.

Accepted Answer

David Young
David Young on 15 Dec 2014
Edited: David Young on 15 Dec 2014
First, let's confirm that the code you have used for the DFT is correct. Simplifying it a little for clarity (the second subscripts are unnecessary for vectors), we can try it on some test data like this:
N = 20; % length of test data vector
data = rand(N, 1); % test data
X = zeros(N,1); % pre-allocate result
for k = 1:N
X(k) = 0;
for n = 1:N
X(k) = X(k)+(data(n)*exp((-1j)*2*pi*(n-1)*(k-1)/N));
max(abs(X - fft(data))) % how different from built-in FFT?
This will print out a value of order 10^-14 - i.e. the processes are effectively the same, and this simple DFT code works fine.
So what is wrong is something to do with the way you are using it in within your program. I can see a couple of things that are probably incorrect. One is that a is being used as a loop counter and also as the loop limit in the DFT code. The loop limit needs to be the length of the data vector (which is why I've used N instead of a in my example). The second thing is that you assign the results to a variable called X, but you don't seem to make use of that variable anywhere later on - the results are in effect ignored. fftECG is used, but that isn't given a value anywhere.
So inconsistent use of variables looks like a problem here - remember that each variable that you use needs to be given a value. I think it's very difficult to get code right if you cut and paste snippets from elsewhere.
Finally, note that using the simple DFT code will be far far slower than calling fft() for any significant amount of data.
David Young
David Young on 17 Dec 2014
That sounds right. fft() will work on a single value (a scalar) - it just treats it as a vector of length 1, and returns its argument. This is correct mathematically, since the DFT of a scalar is just the scalar itself.

Sign in to comment.

More Answers (1)

Piotr Gregor
Piotr Gregor on 1 Jul 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!