2 creation, deletion, management for vectors and buffers
4 This file is part of a program that implements a Software-Defined Radio.
6 Copyright (C) 2004 by Frank Brickle, AB2KT and Bob McGwier, N4HY
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.
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.
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
22 The authors can be reached by email at
30 The DTTS Microwave Society
37 /*------------------------------------------------------------------------*/
38 /* wrapper around calloc */
41 safealloc(int count, int nbytes, char *tag) {
42 char *p = calloc(count, nbytes);
44 if (tag && *tag) fprintf(stderr, "safealloc: %s\n", tag);
45 else perror("safealloc");
52 safefree(char *p) { if (p) free(p); }
54 /*------------------------------------------------------------------------*/
55 /* allocate/free just vectors */
58 newvec_REAL(int size, char *tag) {
59 return (REAL *) safealloc(size, sizeof(REAL), tag);
63 delvec_REAL(REAL *vec) { if (vec) free((char *) vec); }
66 newvec_IMAG(int size, char *tag) {
67 return (IMAG *) safealloc(size, sizeof(IMAG), tag);
71 delvec_IMAG(IMAG *vec) { if (vec) free((char *) vec); }
74 newvec_COMPLEX(int size, char *tag) {
75 return (COMPLEX *) safealloc(size, sizeof(COMPLEX), tag);
79 delvec_COMPLEX(COMPLEX *vec) { if (vec) free((char *) vec); }
81 /*------------------------------------------------------------------------*/
82 /* buffers (mainly i/o) */
83 /*------------------------------------------------------------------------*/
87 newCXB(int size, COMPLEX *base, char *tag) {
88 CXB p = (CXB) safealloc(1, sizeof(CXBuffer), tag);
93 CXBbase(p) = newvec_COMPLEX(size, "newCXB");
96 CXBsize(p) = CXBwant(p) = size;
97 CXBovlp(p) = CXBhave(p) = CXBdone(p) = 0;
104 if (CXBmine(p)) delvec_COMPLEX(CXBbase(p));
109 /*------------------------------------------------------------------------*/
113 newRLB(int size, REAL *base, char *tag) {
114 RLB p = (RLB) safealloc(1, sizeof(RLBuffer), tag);
119 RLBbase(p) = newvec_REAL(size, "newRLB");
122 RLBsize(p) = RLBwant(p) = size;
123 RLBovlp(p) = RLBhave(p) = RLBdone(p) = 0;
130 delvec_REAL(RLBbase(p));
135 //========================================================================
136 // return normalization constant
139 normalize_vec_REAL(REAL *v, int n) {
143 for (i = 0; i < n; i++) {
148 REAL scl = 1.0 / big;
149 for (i = 0; i < n; i++) v[i] *= scl;
156 normalize_vec_COMPLEX(COMPLEX *z, int n) {
160 for (i = 0; i < n; i++) {
165 REAL scl = 1.0 / big;
166 for (i = 0; i < n; i++) z[i] = Cscl(z[i], scl);
172 /*------------------------------------------------------------------------*/
173 /*------------------------------------------------------------------------*/
174 /* mostly for debugging when necessary */
177 dump_REAL(FILE *fp, char *head, REAL *ptr, int beg, int fin) {
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]);
186 dump_IMAG(FILE *fp, char *head, IMAG *ptr, int beg, int fin) {
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]);
195 dump_CX(FILE *fp, char *head, COMPLEX *ptr, int beg, int fin) {
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);