FFT of real signals using MATLAB

Status
Not open for further replies.

upasana2007

Junior Member level 1
Joined
Mar 20, 2010
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
india
Activity points
1,414
Hi all,

I am doing a project which involves the calculation of harmonics of a digital signal(from a microcontroller) connected serially to MATLAB through COM1 port.
The code is as follows:
Fs = 1000; % Sampling frequency
T = 1/Fs; % Sample time
L = 1000; % Length of signal
N=2^nextpow2(L);
t = (0:L-1)*T;
ser= serial('COM1','BaudRate',9600,'DataBits', 8 );
fopen(ser);
while(1)
data=fscanf(ser,'%s');
end
fclose(ser);
clear ser
decdata=hex2dec(data);
y=decdata;
Y = fft(y,N)/L;
f = Fs/2*linspace(0,1,N/2);

% Plot single-sided amplitude spectrum.
plot(f,2*abs(Y(1:1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')


However I am getting errors:

Operation timed out before termination is reached---this comes repeatedly, infinitely when i use the while operation.... however removing the loop gives this warning 1 time,,,,followed by an error notification which says Y = fft(y,N)/L is not applicable for serial input

Where am I going wrong?
 

You are reading the serial port incorrectly. Try:
Code:
data=zeros(1,L);
for l=1:L
  data(l)=hex2dec(fscanf(ser,'%s'));
end
Instead of your "while(1)" loop.
 
You are reading the serial port incorrectly. Try:
Code:
data=zeros(1,L);
for l=1:L
  data(l)=hex2dec(fscanf(ser,'%s'));
end
Instead of your "while(1)" loop.

its not working...i am getting the error
In ana ssignment A[l]=B, the size of l and B must be the same
 

"data" is an array of decimals of L columns and 1 row. "data(l)" is 1 element of that array. If the number of elements in your file is larger than 1, you get that error message. Check the size of your data!
 

"data" is an array of decimals of L columns and 1 row. "data(l)" is 1 element of that array. If the number of elements in your file is larger than 1, you get that error message. Check the size of your data!

My 'data' is a continuos stream of 8-bit-wide digital inputs.....therefore 'number of elements in the file' is infinite.....My 'time out error' has been resolved (there was a problem with the physical interfacing)....but the loop is still not working...(as in there is a size mismatch which u pointed out is due to the fact my file has more than 1 data entry)....so any suggestion for this?
 

If I were you, I would try to read just one 8-bit word, check how it looks and then try to read a sequence.
From the matlab documentation:
 

If I were you, I would try to read just one 8-bit word, check how it looks and then try to read a sequence.
From the matlab documentation:

Hi...
ya I tried with a limited lenght of data and it works fine .....now i need to work out for a continuos real time analysis of my power signal...
 

hi, i am working with real time data and have to take fft of this. i am facing same problem. can u tell me how did u get a solution?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…