-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Uploading source code, readme report, & example .wav files that can be backmasked.
0 parents
commit d8023e9
Showing
11 changed files
with
453 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import java.util.EmptyStackException; | ||
|
||
public class ArrayStack implements BKStack | ||
{ | ||
double[] Stack; | ||
int pointer; | ||
|
||
public ArrayStack() //default with initial size of 10 | ||
{ | ||
Stack = new double[10]; | ||
pointer = 0; | ||
} | ||
|
||
public ArrayStack(int size) //manual initializer of custom size | ||
{ | ||
Stack = new double[size]; | ||
pointer = 0; | ||
} | ||
|
||
public void push(double insert) | ||
{ | ||
if(pointer == Stack.length) | ||
resize(); | ||
|
||
Stack[pointer] = insert; | ||
pointer++; | ||
} | ||
|
||
public void resize() | ||
{ | ||
double[] middleMan = Stack.clone(); | ||
Stack = new double[Stack.length *2]; //twice original size | ||
|
||
for(int i = 0; i < middleMan.length; i++) | ||
Stack[i] = middleMan[i]; | ||
} | ||
|
||
public double pop() | ||
{ | ||
if(!isEmpty()) | ||
{ | ||
pointer--; | ||
return Stack[pointer]; | ||
} | ||
else | ||
{ | ||
System.out.println("ERROR, poping empty ArrayStack"); //EmptyStackException; I realize I could use a Try/Catch, but I think the if's look better here. | ||
System.exit(1); | ||
return -1; | ||
} | ||
} | ||
|
||
public double peek() | ||
{ | ||
return Stack[pointer -1]; | ||
} | ||
|
||
public boolean isEmpty() | ||
{ | ||
if(pointer > 0) | ||
return false; | ||
else | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import java.util.Arrays; | ||
import java.util.EmptyStackException; | ||
import java.util.Iterator; | ||
|
||
|
||
public class ArrayStackIterable implements Iterable<Double> | ||
{ | ||
Double[] Stack; | ||
int pointer; | ||
|
||
public ArrayStackIterable() //default with initial size of 10 | ||
{ | ||
Stack = new Double[10]; | ||
pointer = 0; | ||
} | ||
|
||
public void push(double insert) | ||
{ | ||
if(pointer == Stack.length) | ||
resize(); | ||
|
||
Stack[pointer] = (Double)(insert); | ||
pointer++; | ||
} | ||
|
||
public void resize() | ||
{ | ||
Double[] middleMan = Stack.clone(); | ||
Stack = new Double[Stack.length *2]; //twice original size | ||
|
||
for(int i = 0; i < middleMan.length; i++) | ||
Stack[i] = middleMan[i]; | ||
} | ||
|
||
public double pop() | ||
{ | ||
Iterator I = iterator(); | ||
|
||
if(!I.hasNext()) | ||
{ | ||
pointer--; | ||
return (double)(Stack[pointer]); | ||
} | ||
else | ||
{ | ||
System.out.println("ERROR, poping empty ArrayStack"); //EmptyStackException; i realise i could use a try/catch here, but i think this makes more sense. | ||
System.exit(1); | ||
return -1; | ||
} | ||
} | ||
|
||
public double peek() | ||
{ | ||
return Stack[pointer -1]; | ||
} | ||
|
||
public boolean isEmpty() | ||
{ | ||
if(pointer > 0) | ||
return false; | ||
else | ||
return true; | ||
} | ||
|
||
@Override | ||
public Iterator<Double> iterator() | ||
{ | ||
Iterator<Double> I = Arrays.stream(Stack).iterator(); | ||
return I; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/** | ||
* Interface for a stack of primitive doubles. | ||
* | ||
* NOTE: You will need to provide much better descriptive | ||
* comments than the ones provided in this interface | ||
*/ | ||
public interface BKStack { | ||
|
||
/** | ||
* is empty? | ||
*/ | ||
public boolean isEmpty(); | ||
|
||
/** | ||
* push | ||
*/ | ||
public void push(double d); | ||
|
||
/** | ||
* pop | ||
* | ||
* @return the deleted value | ||
* @throws EmptyStackException if stack is empty | ||
* | ||
*/ | ||
public double pop(); | ||
|
||
/** | ||
* peek | ||
* | ||
* @throws EmptyStackException f stack is empty | ||
* i | ||
*/ | ||
public double peek(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import java.util.EmptyStackException; | ||
|
||
public class ListStack implements BKStack | ||
{ | ||
ListStackNode root; | ||
|
||
public ListStack() | ||
{ | ||
root = new ListStackNode(-1, null); //dummy values for a end-of-line node | ||
} | ||
|
||
public void push(double input) | ||
{ | ||
ListStackNode prev = root; | ||
root = new ListStackNode(input, prev); | ||
} | ||
|
||
public double pop() | ||
{ | ||
if(root.next != null) | ||
{ | ||
double returner = root.data; | ||
root = root.next; | ||
return returner; | ||
} | ||
else | ||
{ | ||
System.err.println("EmptyStackException"); | ||
return -1; | ||
} | ||
} | ||
|
||
public double peek() | ||
{ | ||
if(!isEmpty()) | ||
return root.data; | ||
else | ||
return -1; | ||
} | ||
|
||
public boolean isEmpty() | ||
{ | ||
if(root.next == null) | ||
return true; | ||
else | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
public class ListStackIterable implements Iterable<List> | ||
{ | ||
ListStackNode root; | ||
|
||
public ListStackIterable() | ||
{ | ||
root = new ListStackNode(-1, null); //dummy values for a end-of-line node | ||
} | ||
|
||
public void push(double input) | ||
{ | ||
ListStackNode prev = root; | ||
root = new ListStackNode(input, prev); | ||
} | ||
|
||
public double pop() | ||
{ | ||
if(root.next != null) | ||
{ | ||
double returner = root.data; | ||
root = root.next; | ||
return returner; | ||
} | ||
else | ||
{ | ||
System.err.println("EmptyStackException"); | ||
return -1; | ||
} | ||
} | ||
|
||
public double peek() | ||
{ | ||
if(!isEmpty()) | ||
return root.data; | ||
else | ||
return -1; | ||
} | ||
|
||
public boolean isEmpty() | ||
{ | ||
if(root.next == null) | ||
return true; | ||
else | ||
return false; | ||
} | ||
|
||
@Override | ||
public Iterator<List> iterator() | ||
{ | ||
Iterator<List> I = null; | ||
return I; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
public class ListStackNode | ||
{ | ||
double data; | ||
ListStackNode next; | ||
|
||
public ListStackNode(double input, ListStackNode prev) | ||
{ | ||
this.data = input; | ||
this.next = prev; | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import java.util.List; | ||
|
||
public class Tester | ||
{ | ||
public static void main(String[] args) | ||
{ | ||
//System.out.println("It Works!"); | ||
|
||
/*ArrayStack S = new ArrayStack(); | ||
S.push(1.2); | ||
S.push(2.5); | ||
//System.out.println(S.pop() + " " + S.peek()); | ||
S.push(3.14); | ||
System.out.println(S.pop() + " " + S.pop() + " " + S.pop()); | ||
*/ | ||
|
||
ListStack S = new ListStack(); | ||
S.push(1.2); | ||
S.push(2.5); | ||
System.out.println(S.peek()); | ||
S.push(3.14); | ||
System.out.println(S.pop() + " " + S.pop() + " " + S.pop()); | ||
} | ||
} |
Binary file not shown.
Binary file not shown.