summaryrefslogtreecommitdiff
path: root/macros/modulate.sci
blob: bbce31afe0fb2de4d2be411eea2a92838c2de986 (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
function [y,t]=modulate(x,fc,fs,method,opt)
//Modulates signal according to the modulation method
//Calling Sequence
//y=modulate(x,fc,fs,method,opt)
//[y,t]=modulate(x,fc,fs,method,opt)
//Parameters
//x
//A vector or a matrix
//fc
//Carrier frequency
//fs
//Sampling frequency
//method
//Modulation Method
//opt
//An optional parameter required by certain modulation methods
//Description
//[y,t]=modulate(x,fc,fs,method,opt)
//Returns the modulated vector y and the time vector 't'
//Modulation is performed according to the following table
//   METHOD              MODULATION SCHEME
//    'am',      Amplitude modulation, double side-band, suppressed carrier
//    'amdsb-sc' opt not used. This is the default method.
//    'amdsb-tc' Amplitude modulation, double side-band, transmitted carrier
//               opt is a scalar subtracted from x before multiplying x 
//               with the carrier wave.  It defaults to min(min(x)) so that
//               the input signal after offset is always non-negative
//    'amssb'    Amplitude modulation, single side-band
//               OPT not used.
//    'fm'       Frequency modulation
//               opt is the constant of frequency modulation.
//               opt = (fc/fs)*2*pi/max(max(abs(x))) by default
//    'pm'       Phase modulation
//               OPT is the constant of phase modulation. 
//               opt = pi/max(max(abs(x))) by default 
//               Phase lies between -pi and +pi
//    'pwm'      Pulse width modulation
//               opt='left' corresponds to left justified pulses.
//               opt='centered' correspondes to centered pulses.
//               The default value of opt is 'left'.
//    'ppm'      Pulse position modulation
//               opt is a scalar between 0 and 1 which specifies the pulse 
//               width in fractions of the carrier period with default value 0.1.
//   'qam'       Quadrature amplitude modulation
//               opt is a matrix of the same size as X which is modulated in 
//               quadrature with x.
//
//   If x is a matrix, its columns are modulated.
//Example
//y  =
// 
//    1.    1.    0.    0.  
//Author
//Ankur Mallick
    funcprot(0);
    if (argn(2)<3|argn(2)>5) then
        error('Incorrect number of input arguments.');
    elseif (isreal(fc)&isreal(fs)&fc>fs/2)
        error('The career frequency must be less than half the sampling frequency.')
    else
        flag1=0;
        flag2=0;
        if (argn(2)<4)
            method='am';
        end
        [M,N]=size(x);
        if(M==1)
            flag1=1;
            x=x(:);
            [M,N]=size(x);
        end
        t=((0:M-1)/fs)';
        t=t*ones(1,N);
        if(method=='am'|method=='amdsb-sc')
            y=x.*cos(2*%pi*fc*t);
        elseif(method=='amdsb-tc')
            if(argn(2)<5)
                opt=min(min(x));
            end
            y=(x-opt).*cos(2*%pi*fc*t);
        elseif(method=='amssb')
            y=x.*cos(2*%pi*fc*t)+imag(hilbert(x)).*sin(2*%pi*fc*t);
        elseif(method=='fm')
            if(argn(2)<5)
                opt=max(abs(x(:))); //if all elements of x are zero
                if(opt>0)
                    opt=(fc/fs)*2*%pi/opt;
                end
            end
            y=cos(2*%pi*fc*t + opt*cumsum(x,1));
        elseif(method=='pm')
            if(argn(2)<5)
                opt=%pi/(max(abs(x(:))));
            end
            y=cos(2*%pi*fc*t + opt*x);
        elseif(method=='qam')
            if(argn(2)<5)
                error('For qam, a 5th input parameter is required.')
            else
                if(size(opt,1)==1)
                    opt=opt(:);
                end
                S=sum(abs(size(opt)-size(x))); //S=0 only if opt and x have the same size
                if(S==0)
                    y = x.*cos(2*%pi*fc*t) + opt.*sin(2*%pi*fc*t)
                else
                    error('For qam input signals must be the same size') 
                end
            end
        elseif(method=='pwm')
            if(argn(2)<5)
                opt='left'
            end
            if(max(x(:))>1|min(x(:))<0)
                error('x must lie between 0 and 1');
            else
                t=(0:(M*fs/fc-1))';
                t=t*ones(1,N);
                T=fs/fc;
                y=zeros(t);
                if(opt~='left'&opt~='centered')
                    error('5th input parameter not recognized');
                else
                    if(opt=='left')
                        compar=(pmodulo(t,T))/T;
                        pos=floor(t/T)+1;
                    elseif(opt=='centered')
                        compar1=2*pmodulo(t,T)/T;
                        compar=min(compar1,2-compar1);
                        x(M+1,:)=zeros(1,N);
                        pos=floor(t/T)+1;
                        r1=ceil(T/2);
                        r2=floor(T/2);
                        pos=[pos(r1+1:length(pos));(M+1)*ones(r2,1)];
                    end
                    for i=1:N
                        //x1=matrix(ones(T,1)*x(:,i)',size(t,1),1);
                        x1=x(pos,i)
                        y(compar<x1,i)=1;
                    end 
                end
            end
        elseif(method=='ppm'|method=='ptm')
            if(argn(2)<5)
                opt=0.1
            end
            t=(0:(M*fs/fc-1))';
            t=t*ones(1,N);
            T=fs/fc;
            y=zeros(t);
            if(max(x(:))>1|min(x(:))<0)
                error('x must lie between 0 and 1');
            elseif(~isscalar(opt)|opt<0|opt>1)
                error('opt must be a scalar between 0 and 1')
            else
                if (max(x) > 1-opt)
                    warning('Overlapping pulses')
                end
                for i=1:N
                    x1=x(:,i)'
                    start=1+ceil((x1+(0:M-1))*T); //y(1) corresponds to t=0
                    L=floor(opt*T);  
                    v=(0:1:L-1)';
                    p1=matrix(ones(L,1)*start,1,M*L)
                    p2=matrix(v*ones(1,M),1,M*L);
                    pos=p1+p2;
                    y(pos,i)=1;
                end
                y(length(t)+1:length(y),:)=[]; //Truncating vector
            end
        else
            error('Method not recognised');
            flag2=1;
        end
        if(flag2==0)
            t=t(:,1); //only first column required
            if(flag1==1)
                //x is a row vector
                y=conj(y');
                t=t';
            end
        end
    end
endfunction