forked from MatCat/csv2arb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharb2csv.c
136 lines (113 loc) · 3.06 KB
/
arb2csv.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
// https://github.com/gnbl/csv2arb
//
// Convert <input> 16 bit binary data to text <outfile> with one value per line.
// Detects and removes Point marker bit from data.
// Tested with Rigol DS1000Z-S series built-in arbitrary function generator
//
// Input range: 0 ... 16383 (14 bit)
// Output range: -1.0 ... 1.0
//
// Compile on Windows using MinGW
// - Install MinGW http://sourceforge.net/projects/mingw/files/Installer/
// - Add <INSTALLPATH>/MinGW/bin/ to system PATH (to avoid a dialog window with this error message: "The program can't start because libgmp-10.dll is missing from your computer.")
// - run "gcc arb2csv.c -o arb2csv.exe"
// Based on https://github.com/MatCat/csv2arb but largely modified.
#include <stdio.h>
#include <stdlib.h>
#include "stdint.h"
// Input file value options (for Rigol DS1000Z-S)
#define SAMPLES 16384L
#define DACBITS 14
#define FILEBYTES 2
#define OUTMAX ((1<<DACBITS)-1)
#define OUTZERO (OUTMAX/2)
#define OUTMIN 0x0000
// Output (normalized)
#define INMIN -1.0
#define INMAX 1.0
// map value to DAC range
float //
map(
long x, //
long in_min, //
long in_max, //
float out_min, //
float out_max //
)
{
if(x > in_max)
{
printf("WARNING: value out of range (%d > %d). The input should be normalized.\n", x, in_max);
}
if(x < in_min)
{
printf("WARNING: value out of range (%d < %d). The input should be normalized.\n", x, in_min);
}
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void //
printHelp(
char *argv[] //
)
{
printf("Usage: %s <input> <output>\n",argv[0]);
}
int //
main(
int argc, //
char *argv[] //
)
{
if (argc < 3)
{
printf("ARB to CSV Converter\n\nThis program is free and open source (https://github.com/gnbl/csv2arb, based on csv2arb by MatCat)\n");
printf("\nPlease specify an input and output file.\n");
printHelp(argv);
}
else
{
// open files
printf("\nInput File: %s\n",argv[1]);
FILE *ifp, *ofp;
char *mode = "rb";
char *oMode = "w";
ifp = fopen(argv[1], mode);
ofp = fopen(argv[2], oMode);
if( ifp == NULL )
{
fprintf(stderr, "Error opening %s\n", argv[1]);
exit(1);
}
if( ofp == NULL )
{
fprintf(stderr, "Error opening %s\n", argv[2]);
exit(1);
}
// read
uint16_t data[SAMPLES];
fread( &data, sizeof(data), 1, ifp);
fclose(ifp);
// convert and write
int n;
for( n=0; n < SAMPLES; n++)
{
uint16_t d = ( data[n] );
// Detect and remove Point marker bit
if( d & 0x8000 )
{
printf("Sample %d is a point\n", n);
d = d & ~0x8000;
}
// map DAC bits to values
float oValue = map( d,
OUTMIN, OUTMAX, // out
INMIN, INMAX // in
);
// write ASCII float to output file
fprintf(ofp, "%f\n", oValue);
}
// close file
fclose(ofp);
}
return 1;
}