Forum Discussion
Altera_Forum
Honored Contributor
11 years agohere is copy:
%NCO bit true model
clear all; close all;
%example NCO parameters
n = 20000; %number of test samples
A = 2^15 - 1; %max amplitude, amplitude resolution 16 bit signed
M = 2^32; %NCO accumulator phase resolution
inc = round(M*.001); %NCO accumulator phase increment
k = 2^12; %lut phase resolution (lut size)
lsb = log2(M) - log2(k); %LSBs discarded when addressing
lut = round(A*exp(j*2*pi*(0:k-1)/k)); %lut, one cycle sine data
ptr = 0;
addr = 0;
for i = 1:n
y(i) = lut(addr+1); %add 1 for matlab LUT
ptr = mod(ptr + inc, M); %phase accumulator(ramp)
addr = round(ptr/2^lsb); %discard LSBs
addr(addr >= k) = addr - k; %check address overflow
end
"inc" can be chosen as Fo/Fs scaled by 2^32 but note there is bug for negative frequency, use positive increment and invert at output or avoid matlab mod function. Note also this nco is based on fixed lut resolution (no intermediate values calculated).