%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AccelDSP 10.1. build 1134 production, compiled 2008-02-12
%
% THIS IS UNPUBLISHED, LICENSED SOFTWARE THAT IS THE CONFIDENTIAL
% AND PROPRIETARY PROPERTY OF XILINX OR ITS LICENSORS
%
% Copyright(c) Xilinx, Inc., 2000-2008, All Rights Reserved.
% Reproduction or reuse, in any form, without the explicit written
% consent of Xilinx, Inc., is strictly prohibited.
%
% User: why
% Machine: BUPT-5461E3D393 (i1586, Windows XP Service Pack 2, 5.01.2600)
% Date: Mon Jun 16 09:28:40 2008
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% AccelWare Function: DDS
% AccelDSP Version: 10.1. build 1134 production
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
qFA = quantizer( 'ufixed', 'floor', 'wrap', [ 22 0 ] );
qout = quantizer( 'fixed', 'floor', 'wrap', [ 10 8 ] );
qLoop=quantizer('ufixed','floor','wrap',[32 0]);
warning off
Latency = 1;
DDS_Frequency = 4;% MHz
DesiredFreq = 0; % MHz
PhaseIn = floor(DesiredFreq/DDS_Frequency*2^22);
if(rem(DesiredFreq,1) == 0)
ConstGain = 1;
else
ConstGain = round(1/rem(DesiredFreq,1));
end
%N_SAMPLES = 0; % NOTE: If the desired frequency is changed N_SAMPLES should be updated with the equation below...
N_SAMPLES = lcm(DDS_Frequency*ConstGain,DesiredFreq*ConstGain)/ConstGain;
if (N_SAMPLES>10000)
contSim = menu(['Required Samples ' num2str(N_SAMPLES) '-- This could take a long time'],'Continue','Stop');
else
contSim = 1;
end
if(contSim == 1)
PhaseStream = quantize(qFA,ones(1,N_SAMPLES+Latency)*PhaseIn);
for n = 1:N_SAMPLES+Latency
[Sine_All(n),Cosine_All(n)] = dds_001(PhaseStream(n));
end
PhaseStreamSum=cumsum(PhaseStream);
inRad=PhaseStreamSum*2*pi/(2^qFA.Format(1));
inRad = mod(inRad(1:end-Latency),2*pi);
fig = figure;
set(fig,'Units','normalized');
upos = [0.1 0.1 0.75 0.8];
set(fig,'Position',upos);
set(fig,'Name','AccelWare::');
set(fig,'NumberTitle','off');
ha=axes('pos',[0 1 1 1],'visible','off','Tag','suptitle');
ht=text(0.5,-0.04,'Direct Digital Synthesizer (DDS)');set(ht,'horizontalalignment','center','fontsize',16,'color','b');
set(ht,'Interpreter','none');
streamSinOut=Sine_All(Latency+1:end);
vecerror_sin = streamSinOut-sin(inRad);
streamCosOut=Cosine_All(Latency+1:end);
vecerror_cos = streamCosOut-cos(inRad);
subplot(3,2,1)
hold off;
plot(inRad,sin(inRad),'.')
hold on;
plot(inRad,streamSinOut,'r.')
grid on;
xlabel('x (Radians)');
ylabel('sin(x)');
legend('Matlab','AccelWare');
legend('boxoff')
subplot(3,2,3)
hold off;
plot(inRad,vecerror_sin)
hold on;
plot(inRad,ones(size(vecerror_sin))*2^-qout.Format(2),'g-','LineWidth',2);
plot(inRad,-ones(size(vecerror_sin))*2^-qout.Format(2),'g-','LineWidth',2);
grid on;
xlabel('x (Radians)');
ylabel('AccelWare-Matlab');
title('Error against Matlabs built in functions');
LSBError = sprintf('1-LSB Error: %3.2e',2^-qout.Format(2));
legend('Error against sin(x)',LSBError)
legend('boxoff')
subplot(3,2,5)
hold off;
tempFFT=abs(fft(streamSinOut,N_SAMPLES));
temp2=sort(tempFFT);
loc=find(tempFFT==0);
minNonZero=find(temp2>0);
if(~isempty(minNonZero))
tempFFT(loc)=temp2(minNonZero(1))*1e-5; % fill in zeros with 100dB below first spur
end
plot(linspace(0,1,N_SAMPLES),20*log10(tempFFT))
hold on;
plot(linspace(0,1,N_SAMPLES),max(20*log10(tempFFT))+20*log10(2^--1),'y','LineWidth',2)
plot(linspace(0,1,N_SAMPLES),max(20*log10(tempFFT))+20*log10(2^--1),'g','LineWidth',2)
grid on;
tooClose = 1;
locC=find(tempFFT == temp2(end-1));
offSet=2;
while (tooClose)
loc2=find(tempFFT == temp2(end-offSet));
if (abs(locC-loc2)<=2)
offSet = offSet+1;
else
tooClose = 0;
end
end
tempString=sprintf('%s','Sine Spurious Free Range: ', num2str(20*log10(temp2(end-1)/temp2(end-offSet))), 'dB');
title(['FFT - ' tempString]);
legend('Sine','Noise floor from input quantization','Noise floor from output quantization','location','northeast');
legend('boxoff')
plot(loc2/N_SAMPLES,20*log10(tempFFT(loc2)),'ro')
axis([0 0.5 20*log10(temp2(end-offSet)*1e-3) 20*log10(temp2(end)*10)]);
subplot(3,2,2)
hold off;
plot(inRad,cos(inRad),'.')
hold on;
plot(inRad,streamCosOut,'r.')
grid on;
xlabel('x (Radians)');
ylabel('cos(x)');
legend('Matlab','AccelWare');
legend('boxoff')
subplot(3,2,4)
hold off;
plot(inRad,vecerror_cos)
hold on;
plot(inRad,ones(size(vecerror_cos))*2^-qout.Format(2),'g-','LineWidth',2);
plot(inRad,-ones(size(vecerror_cos))*2^-qout.Format(2),'g-','LineWidth',2);
grid on;
xlabel('x (Radians)');
ylabel('AccelWare-Matlab');
title('Error against Matlabs built in functions');
LSBError = sprintf('1-LSB Error: %3.2e',2^-qout.Format(2));
legend('Error against cos(x)',LSBError)
legend('boxoff')
subplot(3,2,6)
hold off;
tempFFT=abs(fft(streamCosOut,N_SAMPLES));
temp2=sort(tempFFT);
loc=find(tempFFT==0);
minNonZero=find(temp2>0);
if(~isempty(minNonZero))
tempFFT(loc)=temp2(minNonZero(1))*1e-5; % fill in zeros with 100dB below first spur
end
plot(linspace(0,1,N_SAMPLES),20*log10(tempFFT))
hold on;
plot(linspace(0,1,N_SAMPLES),max(20*log10(tempFFT))+20*log10(2^--1),'y','LineWidth',2)
plot(linspace(0,1,N_SAMPLES),max(20*log10(tempFFT))+20*log10(2^--1),'g','LineWidth',2)
grid on;
tooClose = 1;
locC=find(tempFFT == temp2(end-1));
offSet=2;
while (tooClose)
loc2=find(tempFFT == temp2(end-offSet));
if (abs(locC-loc2)<=2)
offSet = offSet+1;
else
tooClose = 0;
end
end
tempString=sprintf('%s','Cosine Spurious Free Range: ', num2str(20*log10(temp2(end-1)/temp2(end-offSet))), 'dB');
title(['FFT - ' tempString]);
legend('Cosine','Noise floor from input quantization','Noise floor from output quantization','location','northeast');
legend('boxoff')
plot(loc2/N_SAMPLES,20*log10(tempFFT(loc2)),'ro')
axis([0 0.5 20*log10(temp2(end-offSet)*1e-3) 20*log10(temp2(end)*10)]);
end