]> git.rkrishnan.org Git - dttsp.git/blob - jDttSP/bufvec.c
Initial revision
[dttsp.git] / jDttSP / bufvec.c
1 /* bufvec.c
2    creation, deletion, management for vectors and buffers 
3    
4 This file is part of a program that implements a Software-Defined Radio.
5
6 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21
22 The authors can be reached by email at
23
24 ab2kt@arrl.net
25 or
26 rwmcgwier@comcast.net
27
28 or by paper mail at
29
30 The DTTS Microwave Society
31 6 Kathleen Place
32 Bridgewater, NJ 08807
33 */
34
35 #include <bufvec.h>
36
37 /*------------------------------------------------------------------------*/
38 /* wrapper around calloc */
39
40 char *
41 safealloc(int count, int nbytes, char *tag) {
42   char *p = calloc(count, nbytes);
43   if (!p) {
44     if (tag && *tag) fprintf(stderr, "safealloc: %s\n", tag);
45     else perror("safealloc");
46     exit(1);
47   }
48   return p;
49 }
50
51 void
52 safefree(char *p) { if (p) free(p); }
53
54 /*------------------------------------------------------------------------*/
55 /* allocate/free just vectors */
56
57 REAL *
58 newvec_REAL(int size, char *tag) {
59   return (REAL *) safealloc(size, sizeof(REAL), tag);
60 }
61
62 void
63 delvec_REAL(REAL *vec) { if (vec) free((char *) vec); }
64
65 IMAG *
66 newvec_IMAG(int size, char *tag) {
67   return (IMAG *) safealloc(size, sizeof(IMAG), tag);
68 }
69
70 void
71 delvec_IMAG(IMAG *vec) { if (vec) free((char *) vec); }
72
73 COMPLEX *
74 newvec_COMPLEX(int size, char *tag) {
75   return (COMPLEX *) safealloc(size, sizeof(COMPLEX), tag);
76 }
77
78 void
79 delvec_COMPLEX(COMPLEX *vec) { if (vec) free((char *) vec); }
80
81 /*------------------------------------------------------------------------*/
82 /* buffers (mainly i/o) */
83 /*------------------------------------------------------------------------*/
84 /* complex */
85
86 CXB
87 newCXB(int size, COMPLEX *base, char *tag) {
88   CXB p = (CXB) safealloc(1, sizeof(CXBuffer), tag);
89   if (base) {
90     CXBbase(p) = base;
91     CXBmine(p) = FALSE;
92   } else {
93     CXBbase(p) = newvec_COMPLEX(size, "newCXB");
94     CXBmine(p) = TRUE;
95   }
96   CXBsize(p) = CXBwant(p) = size;
97   CXBovlp(p) = CXBhave(p) = CXBdone(p) = 0;
98   return p;
99 }
100
101 void
102 delCXB(CXB p) {
103   if (p) {
104     if (CXBmine(p)) delvec_COMPLEX(CXBbase(p));
105     free((char *) p);
106   }
107 }
108
109 /*------------------------------------------------------------------------*/
110 /* real */
111
112 RLB
113 newRLB(int size, REAL *base, char *tag) {
114   RLB p = (RLB) safealloc(1, sizeof(RLBuffer), tag);
115   if (base) {
116     RLBbase(p) = base;
117     RLBmine(p) = FALSE;
118   } else {
119     RLBbase(p) = newvec_REAL(size, "newRLB");
120     RLBmine(p) = TRUE;
121   }
122   RLBsize(p) = RLBwant(p) = size;
123   RLBovlp(p) = RLBhave(p) = RLBdone(p) = 0;
124   return p;
125 }
126
127 void
128 delRLB(RLB p) {
129   if (p) {
130     delvec_REAL(RLBbase(p));
131     free((char *) p);
132   }
133 }
134
135 //========================================================================
136 // return normalization constant
137
138 REAL
139 normalize_vec_REAL(REAL *v, int n) {
140   if (v && (n > 0)) {
141     int i;
142     REAL big = -MONDO;
143     for (i = 0; i < n; i++) {
144       REAL a = abs(v[i]);
145       big = max(big, a);
146     }
147     if (big > 0.0) {
148       REAL scl = 1.0 / big;
149       for (i = 0; i < n; i++) v[i] *= scl;
150       return scl;
151     } else return 0.0;
152   } else return 0.0;
153 }
154
155 REAL
156 normalize_vec_COMPLEX(COMPLEX *z, int n) {
157   if (z && (n > 0)) {
158     int i;
159     REAL big = -MONDO;
160     for (i = 0; i < n; i++) {
161       REAL a = Cabs(z[i]);
162       big = max(big, a);
163     }
164     if (big > 0.0) {
165       REAL scl = 1.0 / big;
166       for (i = 0; i < n; i++) z[i] = Cscl(z[i], scl);
167       return scl;
168     } else return 0.0;
169   } else return 0.0;
170 }
171
172 /*------------------------------------------------------------------------*/
173 /*------------------------------------------------------------------------*/
174 /* mostly for debugging when necessary */
175
176 void
177 dump_REAL(FILE *fp, char *head, REAL *ptr, int beg, int fin) {
178   int i;
179   FILE *iop = fp? fp : stderr;
180   if (head && *head) fprintf(iop, "dump_REAL: %s\n", head);
181   for (i = beg; i < fin; i++)
182     fprintf(iop, "%5d %g\n", i, ptr[i]);
183 }
184
185 void
186 dump_IMAG(FILE *fp, char *head, IMAG *ptr, int beg, int fin) {
187   int i;
188   FILE *iop = fp? fp : stderr;
189   if (head && *head) fprintf(iop, "dump_REAL: %s\n", head);
190   for (i = beg; i < fin; i++)
191     fprintf(iop, "%5d %g\n", i, ptr[i]);
192 }
193
194 void
195 dump_CX(FILE *fp, char *head, COMPLEX *ptr, int beg, int fin) {
196   int i;
197   FILE *iop = fp? fp : stderr;
198   if (head && *head) fprintf(iop, "dump_CX: %s\n", head);
199   for (i = beg; i < fin; i++)
200     fprintf(iop, "%5d %g %g\n", i, ptr[i].re, ptr[i].im);
201 }
202
203