upasana2007
Junior Member level 1

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?
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?