-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNCO.m
57 lines (57 loc) · 2 KB
/
NCO.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
% Filename: NCO.m 2018-03-01
% Constructor: (<table_width>, <sample_rate>)
% SetFrequency(<desired_frequency>)
%
classdef NCO < handle
properties
ONE_ROTATION = double(2^32) % The type changes dPhase!
SAMPLE_RATE
Frequency
BigCounter
Counter
deltaPhase
tablelength
tablewidth
sintable
costable
mask
index
end % of properties
methods
function self = NCO(val1,val2)
if nargin == 2
if isnumeric(val1)
self.tablewidth = val1;
self.tablelength = bitshift(1,val1);
self.SAMPLE_RATE = double(val2);
self.mask = self.tablelength - 1;
for n = 1:self.tablelength
self.sintable(n) = sin(2.0 * pi * n / self.tablelength);
self.sintable(n) = int8(self.tablelength * self.sintable(n));
self.costable(n) = cos(2.0 * pi * n / self.tablelength);
self.costable(n) = int8(self.tablelength * self.costable(n));
self.Counter = uint32(0);
self.deltaPhase = uint32(0);
self.BigCounter = uint64(0);
self.index = 1;
end % of for loop
else
error('Value must be numeric');
end % of is-numeric
end % of nargin
end % of function class constructor
function self = SetFrequency(self, arg1)
self.Frequency = arg1;
self.deltaPhase = uint32(arg1 * self.ONE_ROTATION / self.SAMPLE_RATE);
end % of function frequency
function self = clock(self)
self.BigCounter = self.BigCounter + uint64(self.deltaPhase);
if self.BigCounter >= intmax('uint32');
self.BigCounter = self.BigCounter - uint64(intmax('uint32'));
end % of Counter size test to allow roll-over
self.Counter = uint32(self.BigCounter);
self.index = bitshift(self.Counter, self.tablewidth - 32) + 1;
% self.index = bitand(self.mask, self.index);
end % of function clk
end % of methods
end % of classdef