Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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:
Example — Reading Binary Data


This example illustrates how you can download the TDS 210 oscilloscope screen display to MATLAB. The screen display data is transferred and saved to disk using the Windows bitmap format. This data provides a permanent record of your work, and is an easy way to document important signal and scope parameters.

Because the amount of data transferred is expected to be fairly large, it is asynchronously returned to the input buffer as soon as it is available from the instrument. This allows you to perform other tasks as the transfer progresses. Additionally, the scope is configured to its highest baud rate of 19,200.

Create a serial port object — Create the serial port object s associated with serial port COM1.
s = serial('COM1');


Configure property values — Configure the input buffer to accept a reasonably large number of bytes, and configure the baud rate to the highest value supported by the scope.
s.InputBufferSize = 50000;
s.BaudRate = 19200;


Connect to the device — Connect s to the oscilloscope. Because the default value for the ReadAsyncMode property is continuous, data is asynchronously returned to the input buffer as soon as it is available from the instrument.
fopen(s)


Write and read data — Configure the scope to transfer the screen display as a bitmap.
fprintf(s,'HARDCOPY:pORT RS232')
fprintf(s,'HARDCOPY:FORMAT BMP')
fprintf(s,'HARDCOPY START')


Wait until all the data is sent to the input buffer, and then transfer the data to the MATLAB workspace as unsigned 8-bit integers.
out = fread(s,s.BytesAvailable,'uint8');


Disconnect and clean up — When you no longer need s, disconnect it from the instrument and remove it from memory and from the MATLAB workspace.
fclose(s)
delete(s)
clear s
 

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top