-
Notifications
You must be signed in to change notification settings - Fork 0
/
mexSaveAgilentTraces.c
135 lines (118 loc) · 3.73 KB
/
mexSaveAgilentTraces.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
/*
* Copyright 2010-2014 Regents of the University of Minnesota.
* This file subject to terms of Creative Commons CC BY-NC 4.0, see LICENSE.html
*
* author(s): Michael Tesch ([email protected]),
*/
/*! \file
* \brief Save agilent traces from an array.
*
* \section Building
*
* Use mtmatpack_setup.m or mtagipack_setup.m
*
* \section Example
\verbatim
>> mexSaveAgilentTraces
Error using mexSaveAgilentTraces
usage: mexSaveAgilentTraces(filename [, mainh, blockh])
>>
>> [t, mainh, blockh] = mexLoadAgilentTraces('/home/tesch/data/data.fid');
>> mexSaveAgilentTraces('/home/tesch/data/data2.fid', t);
>> mexSaveAgilentTraces('/home/tesch/data/data3.fid', t, mainh, blockh);
>>
\endverbatim
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "varian.h"
#include "premexh.h"
#include "mex.h"
#include "mexCommons.h" // for mexStructArray()
/*
* these are identical to what's in mexLoadAgilentTraces.c ...
*/
#define NARRAY(a) ((sizeof a) / sizeof (a)[0])
#define MH_FIELD_DEF(fname, ftype) {#fname, (size_t)&((vmain_header_t*)NULL)->fname, \
sizeof(((vmain_header_t*)NULL)->fname), ftype}
#define BH_FIELD_DEF(fname, ftype) {#fname, (size_t)&((vblock_header_t*)NULL)->fname, \
sizeof(((vblock_header_t*)NULL)->fname), ftype}
/*! descriptions of all fields in an varian main header
*/
static struct_fielddesc_t mheader_fields[] = {
MH_FIELD_DEF(nblocks, TY_S32),
MH_FIELD_DEF(ntraces, TY_S32),
MH_FIELD_DEF(np, TY_S32),
MH_FIELD_DEF(ebytes, TY_S32),
MH_FIELD_DEF(tbytes, TY_S32),
MH_FIELD_DEF(bbytes, TY_S32),
MH_FIELD_DEF(status, TY_U16),
MH_FIELD_DEF(vers_id, TY_U16),
MH_FIELD_DEF(nbheaders, TY_S32),
};
/*! descriptions of all fields in an varian block header
*/
static struct_fielddesc_t bheader_fields[] = {
BH_FIELD_DEF(status, TY_U16),
BH_FIELD_DEF(scale, TY_U16),
BH_FIELD_DEF(mode, TY_U16),
BH_FIELD_DEF(index, TY_U16),
BH_FIELD_DEF(ctcount, TY_S32),
BH_FIELD_DEF(lpval, TY_FLOAT),
BH_FIELD_DEF(rpval, TY_FLOAT),
BH_FIELD_DEF(lvl, TY_FLOAT),
BH_FIELD_DEF(tlt, TY_FLOAT),
};
#ifdef DOXYGEN
#define mexFunction mexSaveAgilentTraces
#endif
/* mexSaveAgilentTraces(filename) */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
char * usage = "usage: mexSaveAgilentTraces(filename, fids [, mainh, blockh])\n";
char measfile[512];
const mwSize * dims;
float *outdataR;
float *outdataI;
varian_printf = mexCommonsPrintfCallback;
if ((nrhs != 2 && nrhs != 4)
|| nlhs != 0 || !mxIsChar(prhs[0]) || !mxIsComplex(prhs[1])
|| !mxIsSingle(prhs[1]))
mexErrMsgTxt(usage);
if (mxGetString(prhs[0], measfile, sizeof(measfile)))
mexErrMsgTxt(usage);
dims = mxGetDimensions(prhs[1]);
mwSize nblocks;
if (3 == mxGetNumberOfDimensions(prhs[1]))
nblocks = dims[2];
else if (2 == mxGetNumberOfDimensions(prhs[1]))
nblocks = 1;
else
mexErrMsgTxt(usage);
outdataR = mxGetData(prhs[1]);
outdataI = mxGetImagData(prhs[1]);
mwSize nent = dims[0] * dims[1] * nblocks;
float2 * traces = malloc(sizeof(float2) * nent);
mwSize ii;
for (ii = 0; ii < nent; ii++) {
traces[ii].x = outdataR[ii];
traces[ii].y = outdataI[ii];
}
/* save scandata */
if (nrhs == 2)
varian_save_fid(measfile, dims[1], dims[0], nblocks, traces);
else {
mwSize count;
vmain_header_t * mainh = mexArrayToStruct(NARRAY(mheader_fields), mheader_fields, prhs[2], &count);
vblock_header_t * blockh = mexArrayToStruct(NARRAY(bheader_fields), bheader_fields, prhs[3], &count);
vfile_info_t vinfo;
memcpy(&vinfo, mainh, sizeof(vmain_header_t));
vinfo.main_header.np *= 2;
vinfo.block_headers = blockh;
varian_save_fids(measfile, &vinfo, traces);
free(mainh);
free(blockh);
}
}