]> Creatis software - CreaPhase.git/blob - octave_packages/communications-1.1.1/amodce.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / communications-1.1.1 / amodce.m
1 ## Copyright (C) 2003 David Bateman
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'amdsb-tc',offset)
18 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'amdsb-sc')
19 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'amssb')
20 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'amssb/time',@var{num},@var{den})
21 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'qam')
22 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'fm',@var{dev})
23 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},@var{Fs},'pm',@var{dev})
24 ## @deftypefnx {Function File} {@var{y} =} amodce (@var{x},[@var{Fs},@var{iphs}],@var{...})
25 ##
26 ## Baseband modulator for analog signals. The input signal is specified by
27 ## @var{x}, its sampling frequency by @var{Fs} and the type of modulation
28 ## by the third argument, @var{typ}. The default values of @var{Fs} is 1 and 
29 ## @var{typ} is 'amdsb-tc'.
30 ##
31 ## If the argument @var{Fs} is a two element vector, the the first element
32 ## represents the sampling rate and the second the initial phase.
33 ##
34 ## The different types of modulations that are available are
35 ##
36 ## @table @asis
37 ## @item 'am'
38 ## @itemx 'amdsb-tc'
39 ## Double-sideband with carrier
40 ## @item 'amdsb-sc'
41 ## Double-sideband with suppressed carrier
42 ## @item 'amssb'
43 ## Single-sideband with frequency domain Hilbert filtering
44 ## @item 'amssb/time'
45 ## Single-sideband with time domain filtering. Hilbert filter is used by 
46 ## default, but the filter can be specified
47 ## @item 'qam'
48 ## Quadrature amplitude modulation
49 ## @item 'fm'
50 ## Frequency modulation
51 ## @item 'pm'
52 ## Phase modulation
53 ## @end table
54 ##
55 ## Additional arguments are available for the modulations 'amdsb-tc', 'fm, 
56 ## 'pm' and 'amssb/time'. These arguments are
57 ##
58 ## @table @code
59 ## @item offset
60 ## The offset in the input signal for the transmitted carrier.
61 ## @item dev
62 ## The deviation of the phase and frequency modulation
63 ## @item num
64 ## @itemx den
65 ## The numerator and denominator of the filter transfer function for the
66 ## time domain filtering of the SSB modulation
67 ## @end table
68 ##
69 ## @end deftypefn
70 ## @seealso{ademodce,dmodce}
71
72 function y = amodce (x, Fs, typ, varargin)
73
74   if (nargin < 1)
75     help("amodce");
76   elseif (nargin < 2)
77     Fs = 1;
78     typ = "am";
79   elseif (nargin < 3)
80     typ = "am";
81   endif
82
83   if (isempty(Fs))
84     Fs = 1;
85     iphs = 0;
86   elseif (isscalar(Fs))
87     iphs = 0;
88   else
89     if ((max(size(Fs)) != 2) || (min(size(Fs)) != 1))
90       error ("amodce: sampling frequency must be a scalar or 2-element vector");
91     endif
92     Fs = Fs(1);
93     iphs = Fs(2);
94   endif
95
96   ## Pass the optional arguments
97   offset = min(x(:));
98   dev = 1;
99   num = [];
100   den = [];
101   narg = 1;
102   if (!ischar(typ))
103     error ("amodce: modulation type must be a string");
104   elseif (strcmp(typ,"am") || strcmp(typ,"amdsb-tc"))
105     if (length(varargin) > 0)
106       offset = varargin{1};
107       narg = narg + 1;
108     endif
109   elseif (strcmp(typ,"fm") || strcmp(typ,"pm"))
110     if (length(varargin) > 0)
111       dev = varargin{1};
112       narg = narg + 1;
113     endif
114   endif    
115   if (length(varargin) == narg)
116     error ("amodce: must specify must numerator and denominator of transfer function");
117   elseif (length(varargin) == narg+1)
118     num = varargin{narg};
119     den = varargin{narg+1};
120   elseif (length(varargin) != narg - 1)
121     error ("amodce: too many arguments");
122   endif
123
124   if (strcmp(typ,"am") || strcmp(typ,"amdsb-tc"))
125     y = (x + offset) * exp(1i * iphs);
126   elseif (strcmp(typ,"amdsb-sc"))
127     y = x * exp(1i * iphs);
128   elseif (strcmp(typ,"amssb"))
129     if (!isreal(x))
130       error ("amodce: SSB modulated signal must be real");
131     endif
132     ## Damn, must treat Hilbert transform row-by-row!!!
133     y = zeros(size(x));
134     for i=1:size(x,2)
135       y(:,i) = hilbert(x(:,i)) * exp(1i * iphs);
136     end
137   elseif (strcmp(typ,"amssb/time"))
138     if (isempty(num) || isempty(dem))
139       error ("amodce: have not implemented Hilbert transform in time domain yet");
140     endif
141     y = zeros(size(x));
142     for i=1:size(x,2)
143       y(:,i) = filter(num, den, x(:,i));
144       y(:,i) = (x(:,i) + 1i*y(:,i)) * exp(1i * iphs);
145     end
146   elseif (strcmp(typ,"qam"))
147     if (isreal(x))
148       if (floor(size(x,2)/2) != (size(x,2)/2))
149               error ("amodce: QAM modulation must have an even number of columns for real signals");
150       endif
151       y = (x(:,1:2:size(x,2)) + 1i * x(:,2:2:size(x,2))); 
152     else
153       y = x;
154     endif
155     y = y * exp(1i * iphs);
156   elseif (strcmp(typ,"pm"))
157     y = exp(1i * (dev*x + iphs));
158   elseif (strcmp(typ,"fm"))
159     ## To convert to PM signal, need to evaluate 
160     ##    p(t) = \int_0^t dev * x(T) dT 
161     ## As x(t) is discrete and not a function, the only way to perform the
162     ## above integration is with Simpson's rule. Note \Delta T = 2 * pi / Fs.
163     pm = pi / Fs * dev * (cumsum([zeros(1,size(x,2));x(1:size(x,1)-1,:)]) ...
164                                             + cumsum(x));
165     y = exp(1i * (pm + iphs));
166   else
167     error ("amodce: unknown modulation specified");
168   endif
169
170 endfunction
171