summaryrefslogtreecommitdiff
path: root/idinput.sci
blob: 7fd62e64f653978d323f940e80048cb01c713b39 (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
function[u]=idinput(N,types,band,levels)
// generates random binary input signal
// 
// Calling Seqence
// u=idinput(N);
// u=idinput([n,nu])
// u=idinput([n,nu,m])
// u=idinput(__,type)
// u=idinput(__,type,band)
// u=idinput(__,type,band,levels)

// 
// Parameters
// N       : no of binary signals 
// [n,nu]  : nu channel signal of length n.
// [n,nu,m]: nu channel signal of length nxm and period m.
// type    : "rbs" ,"prbs"
// band    : frequency band of the signal.
// levels  : binary output values
//
// Description   
// u = idinput(n) returns a single-channel random binary signal u of length N. By default it’s values are either -1 or 1.
// u = idinput([n,nu]) returns an nu-channel random binary input signal, where each channel signal has length n,the 
// signals in each channel are independent from each other.
// u = idinput([n,nu,m]) returns an Nu-channel periodic random binary input signal with specified period and number of 
// periods. Each input channel signal is of length m*n.
// u=idinput(___,type) specifies the type of input to be generated:
        // 'rbs' — Random binary signal.
        // 'prbs'—pseudorandom binary signal. 
// u =idinput(___,type,band) specifies the frequency band of the signal.
// u =idinput(___,type,band,levels) specifies the level of the binary generated signal
//
//    
// Examples
// u=idinput([20,2,2],'rbs',[0 0.3],[-1 2]);
// 
// Author
// Ayush Kumar
    
    
    [lhs,rhs]=argn(0)
    if  rhs<4 then                    // checking the input given with function 
        levels=[-1 1]                 // arguments and allocating default value if required
    end
    if  rhs<3 then
        band=[0 0.5]
    end
    if  rhs<2 then
        types='rbs'
    end
    if  size(N,2)==3 then
        P=N(1);
        nu=N(2);
        M=N(3);
    elseif  size(N,2)==2 then
        P=N(1);
        nu=N(2);
        M=1;
    elseif  size(N,2)==1 then
        P=N;
        nu=1;
        M=1;
    else
        error('erroininputargument');
    end

    if levels(2)<levels(1) then           // error checking
        error('levels (1)should be less then levels(2)');
        end
    if type(types)~=10 then
        error('types can be rbs only input valid argument');
        end
    if(band(2)>1) then
        error('band should be in b/w 0-1');
    end
    
    function[vec]=randnu(NN,nuu)   // nuu channel random normally distributed signal generator
    [lhs,rhs]=argn(0)
    if  rhs<2 then                
        nuu=1                      
    end
    rand('seed',getdate('s'))
    for j=1:1:nuu
        for i= 1:NN
            vec(i,j)=rand(NN,'normal')
        end
    end
    endfunction


    if convstr(types)=='rbs' then
        u=randnu(5*P,nu);
        band=band/2;
        if ~and(band==[0 0.5]) then
            if(band(1)==0) then
                [hz]=iir(8,'lp','butt',[band(2) band(1)],[0 0]);  // 8th order butterwoth filter
                num=hz(2);
                den=hz(3);
                for i=1:1:nu
                    y(:,i)=filter(num,den,u(:,i));
                end
            elseif(band(2)==0.5) then
                [hz]=iir(8,'hp','butt',[band(1) band(2)],[0 0]);  
                num=hz(2);
                den=hz(3);
                for i=1:1:nu
                    y(:,i)=filter(num,den,u(:,i));
                end
            else
                [hz]=iir(8,'bp','butt',band,[0 0]);  
                num=hz(2);
                den=hz(3);
                for i=1:1:nu
                    y(:,i)=filter(num,den,u(:,i));
                end
            end
            
            
            u = sign(y(2*P+1:3*P,:)); // taking the middle terms
            u = (levels(2)-levels(1))*(u+1)/2+levels(1);// adjusting binary values according to levels
        else
            u = sign(u(2*P+1:3*P,:));
            u = (levels(2)-levels(1))*(u+1)/2+levels(1);
            
        end
    elseif convstr(types)=='prbs' then
        clockP = floor(1/band(2));
        possP = 2.^(3:18)-1;
        P1 = max(possP(P/clockP-possP>=0));
        if isempty(P1)
            P1 = 7;
        end
        n = find(P1==possP)+2;
   
        if (clockP*P1~=P)
            if M>1
                warning(msprintf(gettext("%s: period of prbs signal changed to %d and length change to %d"),"idinput",clockP*P1,P1*M*clockP));
                P = P1*clockP;
            else
                n = min(n+1,18);
                P1 = 2^n -1;
                warning(msprintf(gettext("%s :generated signal is first %d values of a sequence of length %d"),"idinput",P,clockP*P1));
            end
        end
   
        P1 = 2^n-1;
   
        if n<3 || n>18,
            error(msprintf(gettext('Bad conditioning')));
        end
        fi = -ones(n,1);
        if n==3
            ind = [1,3];
        elseif n==4
            ind = [1,4];
        elseif n==5
            ind = [2,5];
        elseif n==6
            ind = [1,6];
        elseif n==7
            ind = [1,7];
        elseif n==8
            ind = [1,2,7,8];
        elseif n==9
            ind = [4,9];
        elseif n==10
            ind = [3,10];
        elseif n==11
            ind = [9,11];
        elseif n==12
            ind = [6,8,11,12];
        elseif n==13
            ind = [9,10,12,13];
        elseif n==14
            ind = [4,8,13,14];
        elseif n==15
            ind = [14,15];
        elseif n==16
            ind = [4,13,15,16];
        elseif n==17
            ind = [14,17];
        elseif n==18
            ind = [11,18];
        end
        for t = 1:clockP:P1*clockP
            u(t:t+clockP-1,1) = ones(clockP,1)*fi(n); // LL%% multivariable !!
            fi = [prod(fi(ind));fi(1:n-1,1)];
        end
        u = (levels(2)-levels(1))*(u+1)/2+levels(1);
   
        u = u(1:P,1);
        if nu >1
            u1 = [u;u];
            shift = floor(P/nu);
            for ku = 2:nu
                u = [u,u1(shift*(ku-1)+1:P+shift*(ku-1))];
            end
        end
    else
        error(msprintf(gettext('type can be rbs or prbs only')));
    end
    if M>1 then              // generating periodic input if no. of periods>1
        uu = u;
        for i = 2:M
            u = [uu;u];
        end
    end
endfunction