%% Design an NCO Source Block
% This example shows how to design an NCO source block with the following
% specifications:
%
% * Desired output frequency: $${F_0} = 510{\rm{ Hz}} $$
% * Frequency resolution: $$\Delta f = 0.05{\rm{ Hz}} $$
% * Spurious free dynamic range: $$SFDR \ge 90{\rm{ dB}} $$
% * Sample period: $${T_s} = 1/8000{\rm{ s}} $$
% * Desired phase offset: $$\pi /2 $$

% Copyright 2018 The MathWorks, Inc.

%%
% 1. Calculate the number of required accumulator bits from the equation
% for frequency resolution:
%
% $$\Delta f = {1 \over {{T_s} \cdot {2^N}}}{\rm{Hz}}$$
%
% $$0.05 = {1 \over {{1 \over {8000}} \cdot {2^N}}}{\rm{Hz}} $$
%
% $$N = 18 $$
%
% Note that |N| must be an integer value. The value of |N| is rounded up to the
% nearest integer; 18 accumulator bits are needed to accommodate the value
% of the frequency resolution.
%%
% 2. Using this best value of _N_, calculate the frequency resolution that
% will be achieved by the NCO block:
%
% $$\Delta f = {1 \over {{T_s} \cdot {2^N}}}{\rm{Hz}}$$
%
% $$\Delta f = {1 \over {{1 \over {8000}} \cdot {2^{18}}}}{\rm{ Hz}}$$
%
% $$\Delta f = 0.0305$$
%%
% 3. Calculate the number of quantized accumulator bits from the equation
% for spurious free dynamic range and the fact that for a lookup table with
% 2^P entries, _P_ is the number of quantized accumulator bits:
%
% $SFDR = \left( {6P + 12} \right){\rm{dB}}$
%
% $96{\rm{ dB}} = (6P + 12){\rm{ dB}}$
%
% $P = 14$
%%
% 4. Select the number of dither bits. In general, a good choice for the
% number of dither bits is the accumulator word length minus the number of
% quantized accumulator bits; in this case 4.
%%
% 5. Calculate the phase increment:
%
% _phase increment_ =  $${\mathop{\rm round}\nolimits} ({{{F_0} \cdot
% {2^N}} \over {{F_s}}}) $$
%
% _phase increment_ =  $$ {\mathop{\rm round}\nolimits} ({{510 \cdot
% {2^{18}}} \over {8000}}) $$
%
% _phase increment_ = 16712
%%
% 6. Calculate the phase offset, $p_o$, using the desired phase offset, $p_o desired$:
%
% $$p_o = {{{2^{accumulator word length}} \cdot p_o desired} \over {2\pi }}$$
%
% $$p_o = {{{2^{18}} \cdot {\pi  \over 2}} \over {2\pi }}$$
%
% $p_o = 65536$
%%
% 7. Open and simulate the model:
open_system('ex_nco')
sim('ex_nco')
% The NCO block in the model is populated with the specifications and
% quantities you just calculated. The output word length and fraction
% length depend on the constraints of your hardware; this example uses a
% word length of 16 and a fraction length of 14. You can verify that the
% specifications of this problem have been met by looking at the *NCO
% Characterization* pane of the NCO block.
%
% <<nco_characterization.png>>
%%
% 8. Experiment with the model to observe the effects on the output shown
% on the Spectrum Analyzer. For example, try turning dithering on and off,
% and try changing the number of dither bits.


