# Received waveform frequency is way too low than it was sent

I am using Quartus 12.1 sp1, vhdl and Altera Nios II programmed in C code for DE0-Nano Development. Basically, what I have is, data is sent from fpga vhdl block to NIos II system, Nios II system sends the data to serial port, Matlab access the serial port to real time plot the graph.

In my system, the rate the graph is plotted is much slower (11Hz) than the rate the processor speed (50MHZ). So I do some buffering and send the data for plotting afterwards.

I am buffering data received from fpga vhdl say 1000 sample points and save it in sdram... after saving these 1000 sample points, the C code will send data to uart so that Matlab can access these serial port data and plot the graph real time.

My problem is, a complete cycle of sinewave obtained from fpga is with a frequency of 50Hz, so it should be having a period of 0.02 seconds instead of 0.16 minutes as I got now, I don't know what could be the problem? Forgive me, I am newbie to signal processing.. Thank you.

int max_sample = 1000; //number of sample points wanted

if (a_out_data==1) //check if it is the beginning of a sinusoidal waveform?
{

for(i=0;i<max_sample;i++)//start the process of buffering data
{
vref_data = IORD_ALTERA_AVALON_PIO_DATA(vref_base); //read a single value of reference voltage from fpga
*(pSDRAM+i) =vref_data; //save this single value in a location of SDRAM specified in SDRAM_U1_BASE
}
}

for(i2=0;i2<i;i2++) //start the process of sending data to uart for all data saved previously
{
data16=*(pSDRAM+i2); //access each value from SDRAM

//send data to uart before increment i++
}


EDIT: I posted the Matlab code here:

max_packets = 20000; %maximum number of packets to keep in memory (all are saved to log file)
update_plot = 1; %how often we should update the plot
plot_length = 12; %maximum length of plot
figure('Position', [10, 50, 900, 900]); % new figure
ax1 = subplot(3,1,1); % top subplot
ax2 = subplot(3,1,2); % bottom subplot
ax3 = subplot(3,1,3);
drawnow;
s = serial(port);
cleanupObj = onCleanup(@()cleanupFunc(s)); %clean-up function
set(s,'BaudRate',115200,'timeout',2); %timeout is in seconds
fopen(s);
vref= NaN(12,max_packets);
j = 0;
tic; %start timer
while 1; %do forever...
if j < max_packets
j = j+1;
else
fprintf('\n Buffer full, reset to 0\n');
j = 1; %reset counter to stop memory overflow
end
%get data line by line
line = fscanf(s);
[c,vo] = disect_line(line);
if c == 1 %start recording from set 1
vref(1,j) = vo;
for i = 2:12 %get set 2 to 12
line = fscanf(s);
[c,vo] = disect_line(line);
if c == i
vref(i,j) = vo;
else
fprintf('\nError\n');
end
end
dlmwrite(logfile,[toc  vref(:,j).' ],'delimiter',',','-append'); %save data from all 12 sets to csv file
update plot1 every update_plot packets, and after we have at least 2 data points (otherwise we get errors)
if mod(j, update_plot)==0 && j>1
if j < plot_length
start = 1;
else
start = j-plot_length+1;
end
plot(ax1, start:j, vref(1,start:j).');
for k = 1:1
text(j, vref(k, j), num2str(k), 'Parent', ax1);
end
xlim(ax1, [start j]);
ylabel(ax1, 'Vref');
title(ax1, ['Time elapsed = ' num2str(toc) ' s']);
end
end
function [c, vo]  = disect_line(line)
c = str2double(line(1:2)); %set number
vo = str2double(line(4:7));
end
function cleanupFunc(s)
fprintf('\nClosed serial port.\n')
fclose(s);
delete(s);
clear s;
end

• Samplerate mismatch? How does matlab know the time scale of your signal? How do you plot the signal? How do you measure the frequency? – Jazzmaniac Aug 17 '15 at 16:09
• @Jazzmaniac, thank you for your reply. I have updated the Matlab code. My problem is, I can get the amplitude of the samples plotted correctly, means the data were sent correctly to serial port and Matlab can read the values correctly, the issue is with the frequency, but I dont know what actually determine the period of the plotted waveform in Matlab? – user2563812 Aug 18 '15 at 8:45
• And, index j is used for the x axis where j represents the number of packet but does not represent a real time scale for the x axis, could this be the problem? Any idea how to build a time vector based on what is the sampling rate (time gap) between every point? – user2563812 Aug 18 '15 at 8:45
• If you have $N$ samples with a sampling rate of $S$ and a start time $t_0$, then the time axis can be written as t=(0:(N-1))/S+t0 – Jazzmaniac Aug 18 '15 at 9:16