]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/oscillator.c
Initial revision
[dttsp.git] / jDttSP / oscillator.c
1 /* oscillator.c 
2
3 This routine implements a common fixed-frequency oscillator
4
5 This file is part of a program that implements a Software-Defined Radio.
6
7 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22
23 The authors can be reached by email at
24
25 ab2kt@arrl.net
26 or
27 rwmcgwier@comcast.net
28
29 or by paper mail at
30
31 The DTTS Microwave Society
32 6 Kathleen Place
33 Bridgewater, NJ 08807
34 */
35
36 #include <common.h>
37
38 #define HUGE_PHASE 1256637061.43593
39
40 void
41 ComplexOSC(OSC p) {
42   int i;
43   COMPLEX z, delta_z;
44
45   if (OSCphase(p) > HUGE_PHASE) OSCphase(p) -= HUGE_PHASE;
46
47   z = Cmplx(cos(OSCphase(p)), sin(OSCphase(p))),
48   delta_z = Cmplx(cos(OSCfreq(p)), sin(OSCfreq(p)));    
49
50   for (i = 0; i < OSCsize(p); i++)
51     z = CXBdata((CXB) OSCbase(p), i)
52       = Cmul(z, delta_z),
53     OSCphase(p) += OSCfreq(p);
54 }
55
56 #ifdef notdef
57 void
58 ComplexOSC(OSC p) {
59   int i;
60   if (OSCphase(p) > 1256637061.43593)
61     OSCphase(p) -= 1256637061.43593;
62   for (i = 0; i < OSCsize(p); i++) {
63     OSCreal(p, i) = cos(OSCphase(p));
64     OSCimag(p, i) = sin(OSCphase(p));
65     OSCphase(p) += OSCfreq(p);
66   }
67 }
68 #endif
69
70 PRIVATE REAL
71 _phasemod(REAL angle) {
72   while (angle >= TWOPI) angle -= TWOPI;
73   while (angle < 0.0) angle += TWOPI;
74   return angle;
75 }
76
77 void
78 RealOSC(OSC p) {
79   int i;
80   for (i = 0; i < OSCsize(p); i++) {
81     OSCRdata(p, i) = sin(OSCphase(p));
82     OSCphase(p) = _phasemod(OSCfreq(p) + OSCphase(p));
83   }
84 }
85
86 OSC
87 newOSC(int size,
88        OscType TypeOsc,
89        REAL Frequency,
90        REAL Phase,
91        REAL SampleRate,
92        char *tag) {
93   OSC p = (OSC) safealloc(1, sizeof(oscillator), tag);
94   if ((OSCtype(p) = TypeOsc) == ComplexTone)
95     OSCbase(p) = (void *) newCXB(size,
96                                  NULL,
97                                  "complex buffer for oscillator output");
98   else
99     OSCbase(p) = (void *) newRLB(size,
100                                  NULL,
101                                  "real buffer for oscillator output");
102   OSCsize(p) = size;
103   OSCfreq(p) = 2.0 * M_PI * Frequency / SampleRate;
104   OSCphase(p) = Phase;
105   return p;
106 }
107
108 void
109 delOSC(OSC p) {
110   if (p->OscillatorType == ComplexTone)
111     delCXB((CXB) p->signalpoints);
112   else
113     delRLB((RLB) p->signalpoints);
114   if (p)
115     safefree((char *) p);
116 }
117