close all 
clear 
clc 
clear alt_signaltap_run;

% 得到 Window的系统路径到 wp变量
wp = getenv('path'); % wp is windows path
% 设定quartus的路径变量
qp = 'c:\\altera\\13.1\\quartus\\bin64';
% 使用正则表达式
% 检查qp路径是否已经在wp路径中
is_qp_in_path = regexp(wp,qp);
if(isempty(is_qp_in_path))
% 添加qp路径到系统路径
setenv('path', [wp ';' qp]);
end
% 添加qp路径到Matlab路径
addpath(qp);

stp_f_dir = './hdl/prj_q131/';
stp_f_n  = 'stp1.stp'; 
stp_f_dir_n = [stp_f_dir stp_f_n];

N_win = 2048;
window = kaiser(N_win,  8);
N_overlap = N_win/2;

% 根据STP文件的完整路径文件名称
% 运行一次 Signaltap 抓取数据
data_stp = alt_signaltap_run(stp_f_dir_n);
x  = double(data_stp(:,2));
px = pwelch(x,window,N_overlap);
y = 10*log10(px); 
h = plot(y);
grid on
ylim([-40 100]);
while(1)
% 指定h绘图句柄的 Y轴数据源来自向量 y
set(h,'YDataSource','y');

data_stp = alt_signaltap_run(stp_f_dir_n);
x  = double(data_stp(:,2));
px = pwelch(x,window,N_overlap);
y = 10*log10(px); 


% 命令绘图句柄刷新数据源
refreshdata;
% 命令图形曲线立即重绘
drawnow;
end

% 绘图并得到句柄h
% h = plot(y);
% set(h,'YDataSource','y');

