-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackMasking.java
143 lines (123 loc) · 4.86 KB
/
BackMasking.java
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
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Read a .dat file and reverse it.
*
* couresy of Milne @UWashington
*/
public class BackMasking{
//@SuppressWarnings("unused")
public static void main(String[] args)
{
if (args.length != 3)
{
System.err.println(" Incorrect number of arguments");
System.err.println(" Usage: ");
System.err.println("\tjava BackMasking <stack type: list/array> <input file> <output file>");
System.exit(1);
}
boolean useList = true;
if (args[0].compareTo("list") == 0)
useList = true;
else if (args[0].compareTo("array") == 0)
useList = false;
else
{
System.err.println("\tsaw " + args[0] + " instead of list or array as first argument");
System.exit(1);
}
try {
//
// Set up the input file to read, and the output file to write to
//
BufferedReader fileIn = new BufferedReader(new FileReader(args[1]));
PrintWriter fileOut = new PrintWriter(new BufferedWriter(new FileWriter(args[2])));
//
// Read the first line of the .dat file to get sample rate.
// We want to store the sample rate value in a variable,
// but we can ignore the "; Sample Rate" part of the line.
// Step through the first line one token (word) at a time
// using the StringTokenizer. The fourth token is the one
// we want (the sample rate).
//
StringTokenizer str;
String oneLine;
int sampleRate;
String strJunk;
oneLine = fileIn.readLine();
str = new StringTokenizer(oneLine);
strJunk = str.nextToken(); // Read in semicolon
strJunk = str.nextToken(); // Read in "Sample"
strJunk = str.nextToken(); // Read in "Rate"
// Read in sample rate
sampleRate = Integer.parseInt(str.nextToken());
//
// Read in the remainder of the file on line at a time.
// The values in the first column are thrown away.
// Place values from the second column on the stack.
// Stop reading if we reach the end of the file.
//
BKStack s;
if (useList)
s = new ListStack();
else
s = new ArrayStack();
String timestep;
double data;
int count = 0;
while ((oneLine = fileIn.readLine()) != null)
{
if (oneLine.charAt(0) == ';')
{
continue;
}
str = new StringTokenizer(oneLine);
// Read in time step value from first column
timestep = str.nextToken();
// Read in data value from second column
data = Double.parseDouble(str.nextToken());
s.push(data);
count++;
}
System.out.println(count + " samples in file");
//
// Print the data values to output .dat file.
// First, output the header line:
// "; Sample Rate <sample rate>"
//
fileOut.println("; Sample Rate " + sampleRate);
// Since the first column consists of numbers which start
// at 0 and increase by 1/sampleRate every time slice, we'll
// just use numSteps to recalculate these numbers.
int numSteps = 0;
// Finally, we print the values in reverse order (by popping
// them off the stack). The first column consists of numbers
// which start at 0 and increase by 1/sampleRate per row, so
// we'll use numSteps/sampleRate to recalculate the appropriate
// values. Print a tab for uniform spacing.
while (!s.isEmpty())
{
fileOut.println((double) numSteps / sampleRate + "\t" + s.pop());
numSteps++;
}
//
// Close the files
//
fileIn.close();
fileOut.close();
} catch (IOException ioe) {
System.err
.println("Error opening/reading/writing input or output file.");
System.exit(1);
} catch (NumberFormatException nfe) {
System.err.println(nfe.toString());
System.err.println("Error in file format");
System.exit(1);
}
}
}