summaryrefslogtreecommitdiff
path: root/BaudRatePD.m
blob: 3113f7567d608ab3ec5453394be385ce27af84b3 (plain)
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
classdef (StrictDefaults) BaudRatePD < serdes.SerdesAbstractSystemObject & TriggeredComponent
    % BaudRatePD     Baud-Rate Phase Detector
    %   obj = BaudRatePD returns a System Object, obj, that performs a
    %   baud-rate or Meuller-Muller phase detection.
    %
    % BaudRatePD methods:
    %   step - Performs a baud-rate phase detection using the demux'ed input
    %          decisions and samples.  An example usage is as follows:
    %          UpDownOut = step(obj,ClockIn,DecisionIn,SampleIn)
    %
    % BaudRatePD properites:
    %   DemuxWidth - Width of input signals

    %   Copyright 2018-2019 The MathWorks, Inc.

    %#codegen

    properties (Nontunable)

        %Demux Width
        DemuxWidth = 32;
    end
    properties (Hidden, SetAccess=private)
        SamplePrevious      % Last sample   from previous frame
        DecisionPrevious    % Last decision from previous frame
        TimingFunctionValue % Timing function value
        SymmetricTransition % Symmetric transition indicator
        UpDownOutInternal   % Phase error output
    end
    properties(Hidden)
        %Property required by abstract class but unused
        NumberOfClocks = 1;
    end
    properties (SetAccess = immutable, Nontunable, Hidden)
        IsLinear = true;
        IsTimeInvariant = true;
    end
    properties (Nontunable,Hidden)
        %Input Waveform Type
        %   Set the input wave type as one of 'Sample' | 'Impulse' |
        %   'Waveform'.  The default is 'Sample'.
        WaveType = 'Sample';
    end
    properties(Hidden, Constant)
        WaveTypeSet = matlab.system.StringSet({'Sample','Impulse','Waveform'});
    end

    methods
        % Constructor
        function obj = BaudRatePD(varargin)
            % Support name-value pair arguments when constructing object
            obj.BlockName = 'BaudRatePD';
            setProperties(obj,nargin,varargin{:})
        end
    end
    methods (Hidden)
        % The below methods, getAMIParameters, getAMIInputNames and
        % getAMIOutputNames are for use only within the serdesDesigner App
        % and will not influence the AMI parameters in Simulink whatsoever.
        function amiParameters = getAMIParameters(~)
            amiParameters = {};
        end
        function names = getAMIInputNames(~)
            names = {};
        end
        function names = getAMIOutputNames(~)
            names = {};
        end
    end
    methods (Access = protected, Hidden)
        function val = isSample(obj)
            val = strcmpi(obj.WaveType,'Sample');
        end
    end
    methods(Access = protected)
        %% Common functions
        function setupImpl(obj)

            setupClock(obj);

            % Initialize sample and decision from previous frame
            obj.SamplePrevious = 0;
            obj.DecisionPrevious = 0;

            % Initialize timing function and symmetric transition indicator
            obj.TimingFunctionValue = zeros(obj.DemuxWidth, 1);
            obj.SymmetricTransition = zeros(obj.DemuxWidth, 1);

            % Initialize output
            obj.UpDownOutInternal = 0;
        end

        function validateInputsImpl(~,waveIn)
            validateattributes(waveIn,{'numeric'},{'finite'},'','waveIn');
        end

        function UpDownOut = stepImpl(obj,ClockIn,DecisionIn,SampleIn)
            %UpDownOut = stepImpl(obj,ClockIn,DecisionIn,SampleIn)

            %TODO
%             if nargin==4
%                 SampleIn = varargin{1};
%                 ClockIn = varargin{2};
%             else
%                 SampleIn = 0;
%                 ClockIn = 0;
%             end

            if isSample(obj)
                %Triggered clock step
                ClockStep(obj,ClockIn)

                % On falling clock edge, process frame of samples
                if obj.PhaseFallingIndex > 0

                    % Calculate timing function
                    % D[i]*S[i-1] - S[i]*D[i-1]
                    obj.TimingFunctionValue = SampleIn .* [obj.DecisionPrevious; DecisionIn(1:end-1)] - ...
                        DecisionIn .* [obj.SamplePrevious; SampleIn(1:end-1)];

                    % Mark symmetric transitions
                    % D[i] == -D[i-1]
                    obj.SymmetricTransition = double(abs(DecisionIn + [obj.DecisionPrevious; DecisionIn(1:end-1)]) < eps);

                    % Calculate phase error
                    % 1. Slice timing function from every sample in the frame
                    % 2. Only symmetric transitions contribute to phase error
                    % 3. Add contributions of all samples in the frame together
                    obj.UpDownOutInternal = sum(obj.SymmetricTransition .* sign(obj.TimingFunctionValue));

                    % Update previous frame decision and sample
                    obj.DecisionPrevious = DecisionIn(end);
                    obj.SamplePrevious = SampleIn(end);

                end % obj.PhaseFallingIndex > 0
            end

            % Assign output
            UpDownOut = obj.UpDownOutInternal;
        end

        function resetImpl(~)
            % Initialize / reset discrete-state properties
        end

        %% Simulink functions
        function icon = getIconImpl(~)
            % Define icon for System block
            icon = "Baud\nRate\nPhase\nDetector";
        end
        function [name1,name2,name3] = getInputNamesImpl(~)
            name1 = sprintf('Demux\nClock');
            name2 = 'Decision';
            name3 = 'Sample';            
        end
        function name1 = getOutputNamesImpl(~)
            name1 = 'UpDown';
        end
        function num = getNumInputsImpl(obj)
            if isSample(obj)
                num = 3;
            else
                num = 1;
            end
        end
    end
    methods(Static, Access=protected)
        function group = getPropertyGroupsImpl(~)
            % Define property section(s) for System block dialog
            group = matlab.system.display.SectionGroup(...
                'Title','Main',...
                'PropertyList',{'DemuxWidth'});
        end
    end
end