-
Notifications
You must be signed in to change notification settings - Fork 19
/
downsample.c
160 lines (137 loc) · 5.28 KB
/
downsample.c
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
#include <math.h>
#include <string.h>
#include "psrfits.h"
// TODO: for these to work with OpenMP, we probably need
// separate input and output arrays and then a copy.
// Otherwise, the threads will step on each other.
void convert_4bit_to_8bit(unsigned char *indata, unsigned char *outdata, int N)
// This converts 4-bit indata to 8-bit outdata
// N is the total number of data points
{
int ii;
unsigned char uctmp;
// Convert all the data from 4-bit to 8-bit
for (ii = 0 ; ii < N / 2 ; ii++, indata++) {
uctmp = *indata;
*outdata++ = uctmp >> 4; // 1st 4 bits (MSBs) are first nibble
*outdata++ = uctmp & 0x0F; // 2nd 4 bits (LSBs) are second nibble
}
}
void pf_4bit_to_8bit(struct psrfits *pf)
// This converts 4-bit pf->sub.rawdata to 8-bit pf->sub.data
{
convert_4bit_to_8bit((unsigned char *)pf->sub.rawdata,
(unsigned char *)pf->sub.data,
pf->sub.bytes_per_subint * 2);
}
void convert_8bit_to_4bit(unsigned char *indata, unsigned char *outdata, int N)
// This converts 8-bit indata to 4-bit outdata
// N is the total number of data points
{
int ii;
// Convert all the data from 4-bit to 8-bit
for (ii = 0 ; ii < N / 2 ; ii++, outdata++) {
*outdata = *indata++ << 4; // 1st 4 bits (MSBs) are first point
*outdata += *indata++; // 2nd 4 bits (LSBs) are second point
}
}
void pf_8bit_to_4bit(struct psrfits *pf)
// This converts 8-bit pf->sub.data into 4-bit pf->sub.rawdata
{
long long numoutsamp = pf->sub.bytes_per_subint * 2 / \
(pf->hdr.ds_time_fact * pf->hdr.ds_freq_fact);
convert_8bit_to_4bit((unsigned char *)pf->sub.data,
(unsigned char *)pf->sub.rawdata,
numoutsamp);
}
void get_stokes_I(struct psrfits *pf)
/* Move the Stokes I in place so that it is consecutive in the array */
{
int ii;
float *data;
struct hdrinfo *hdr = &(pf->hdr);
const int out_nchan = hdr->nchan / hdr->ds_freq_fact;
// In this mode, average the polns first to make it like IQUV
if (strncmp(hdr->poln_order, "AABBCRCI", 8)==0) {
float *bbptr;
int jj;
for (ii = 0 ; ii < hdr->nsblk ; ii++) {
data = pf->sub.fdata + ii * out_nchan * 4; // 4 polns
bbptr = data + out_nchan;
for (jj = 0 ; jj < out_nchan ; jj++, data++, bbptr++)
*data = 0.5 * (*data + *bbptr); // Average AA and BB polns
}
}
data = pf->sub.fdata;
// Start from 1 since we don't need to move the 1st spectra
for (ii = 1 ; ii < hdr->nsblk ; ii++) {
memcpy(data + ii * out_nchan,
data + ii * 4 * out_nchan,
out_nchan * sizeof(float));
}
}
void downsample_time(struct psrfits *pf)
/* Average adjacent time samples together in place */
/* This should be called _after_ make_subbands() */
{
int ii, jj, kk;
struct hdrinfo *hdr = &(pf->hdr);
float *data = pf->sub.fdata;
float *indata, *outdata, *tmpspec;
const int dsfact = hdr->ds_time_fact;
// Treat the polns as being parts of the same spectrum
int out_npol = hdr->npol;
if (hdr->onlyI) out_npol = 1;
const int in_nchan = hdr->nchan * out_npol;
const int out_nchan = in_nchan / hdr->ds_freq_fact;
const int out_nsblk = hdr->nsblk / dsfact;
const float norm = 1.0 / dsfact;
tmpspec = (float *)malloc(out_nchan * sizeof(float));
indata = data;
// Iterate over the output times
for (ii = 0 ; ii < out_nsblk ; ii++) {
// Initiaize the summation
for (jj = 0 ; jj < out_nchan ; jj++)
tmpspec[jj] = 0.0;
// Add up the samples in time in the tmp array
for (jj = 0 ; jj < dsfact ; jj++) {
outdata = tmpspec;
for (kk = 0 ; kk < out_nchan ; kk++, indata++, outdata++)
*outdata += *indata;
}
// Convert the sum to an average and put into the output array
outdata = data + ii * out_nchan;
for (jj = 0 ; jj < out_nchan ; jj++)
outdata[jj] = tmpspec[jj] * norm;
}
free(tmpspec);
}
void guppi_update_ds_params(struct psrfits *pf)
/* Update the various output data arrays / values so that */
/* they are correct for the downsampled data. */
{
struct hdrinfo *hdr = &(pf->hdr);
struct subint *sub = &(pf->sub);
int out_npol = hdr->npol;
if (hdr->onlyI) out_npol = 1;
int out_nchan = hdr->nchan / hdr->ds_freq_fact;
if (hdr->ds_freq_fact > 1) {
int ii;
double dtmp;
/* Note: we don't need to malloc the subint arrays since */
/* their original values are longer by default. */
// The following correctly accounts for the middle-of-bin FFT offset
dtmp = hdr->fctr - 0.5 * hdr->BW;
dtmp += 0.5 * (hdr->ds_freq_fact - 1.0) * hdr->df;
for (ii = 0 ; ii < out_nchan ; ii++)
sub->dat_freqs[ii] = dtmp + ii * (hdr->df * hdr->ds_freq_fact);
for (ii = 1 ; ii < out_npol ; ii++) {
memcpy(sub->dat_offsets+ii*out_nchan,
sub->dat_offsets+ii*hdr->nchan,
sizeof(float)*out_nchan);
memcpy(sub->dat_scales+ii*out_nchan,
sub->dat_scales+ii*hdr->nchan,
sizeof(float)*out_nchan);
}
}
}