// File: /workspace/github_c_makefile_star2_O2/DrYerzinia[P]C_DigiMode/src/filter/filter_test.cpp
// Function: main
int main(int argc, char **argv){

	std::string args;
	for(int i = 0; i < argc; i++) args += std::string(argv[i]) + std::string(" ");
	util::readInArguments(args);

	std::string fn = util::getArgument("-fn");
	std::string filename = "dat/psk/0_5000hz_sweep.raw";
	if(fn.compare("NULL")){
		filename = fn;
	}

	std::string of = util::getArgument("-of");
	std::string output_filename = "dat/data_filtered.raw";
	if(of.compare("NULL")){
		output_filename = of;
	}

	FIR_filter test_filter;

	// filter parameteres ////////////////////////////////////////////////

	std::string ft = util::getArgument("-ft");
	std::string filter_type = "highpass";
	if(filter_type.compare("NULL")){
		filter_type = ft;
	}
	
	int points = 127;
	float low_cutoff_freq = 800;
	float high_cutoff_freq = 900;
	float cutoff_freq = 2500;

	std::string pts = util::getArgument("-pts");
	if(pts.compare("NULL")){
		points = atoi(pts.c_str());
	}
	
	std::string co = util::getArgument("-co");
	if(co.compare("NULL")){
		cutoff_freq = atof(co.c_str());
	}
	
	std::string lco = util::getArgument("-lco");
	if(lco.compare("NULL")){
		low_cutoff_freq = atof(lco.c_str());
	}


	std::string hco = util::getArgument("-hco");
	if(hco.compare("NULL")){
		high_cutoff_freq = atof(hco.c_str());
	}

	test_filter.set_sample_rate(22050);

	// Setup Filter ///////////////////////////////////////////////////////

	if(filter_type.compare("bandpass") == 0){
		test_filter.set_up_bandpass(points, low_cutoff_freq, high_cutoff_freq);
	} else if(filter_type.compare("lowpass") == 0){
		test_filter.set_up_lowpass(points, cutoff_freq);
	} else if(filter_type.compare("highpass") == 0){
		test_filter.set_up_highpass(points, cutoff_freq);
	}  else if(filter_type.compare("bandstop") == 0){
		test_filter.set_up_bandreject(points, low_cutoff_freq, high_cutoff_freq);
	} else if(filter_type.compare("90deg") == 0){
		test_filter.set_up_90deg();
	} else {
		std::cout << "Invalid Filter Type: " << filter_type << std::endl;
		exit(0);
	}

	// Filter Data and Write Data ////////////////////////////////////////

	std::ostream *output_file;
	if(output_filename.compare("stdout") == 0) output_file = &std::cout;
	else output_file = new std::fstream(output_filename.c_str(), std::fstream::out | std::fstream::binary);

	char wbuf[1];
	float max = 0.0001;

	std::istream *data_file;

	if(filename.compare("stdin") == 0){
		data_file = &std::cin;
	} else {
		data_file = new std::fstream(filename.c_str(), std::fstream::in | std::fstream::binary);

		if(!((std::fstream*)data_file)->is_open()){
			std::cout << "Error: couldn't open input file" << std::endl;
			exit(0);
		}
	}

	while(!data_file->eof()){		
		data_file->read(wbuf, 1);
		if(test_filter.proccess_byte(wbuf[0])){
			float dat = test_filter.get_next_datapoint();
			if(max < dat)
				max = dat;
			wbuf[0] = (char) dat*(127/max);
			output_file->write(wbuf, 1);
			output_file->flush();
		}
	}

	if(filename.compare("stdin") != 0) ((std::fstream*)data_file)->close();

	if(output_filename.compare("stdout") != 0){
		((std::fstream*)output_file)->close();
		delete output_file;
	}

	// The End ////////////////////////////////////////////////////////////

	return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2c8, %rsp            # imm = 0x2C8
movq %rsi, %rbx
movl %edi, %r14d
leaq 0x198(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %r12d, %r12d
testl %edi, %edi
cmovlel %r12d, %r14d
movb $0x0, (%rax)
leaq 0x70(%rsp), %r15
leaq 0x30(%rsp), %rbp
leaq 0x130(%rsp), %r13
cmpq %r12, %r14
je 0x25ea
movq (%rbx,%r12,8), %rsi
movq %r15, %rdi
leaq 0x110(%rsp), %rdx
callq 0x2ee0
movq %rbp, %rdi
leaq 0x2b09(%rip), %rsi      # 0x50ae
leaq 0x50(%rsp), %rdx
callq 0x2ee0
movq %r13, %rdi
movq %r15, %rsi
movq %rbp, %rdx
callq 0x2e79
leaq 0x188(%rsp), %rdi
movq %r13, %rsi
callq 0x2370
movq %r13, %rdi
callq 0x20f0
movq %rbp, %rdi
callq 0x20f0
movq %r15, %rdi
callq 0x20f0
incq %r12
jmp 0x2582
leaq 0x2a8(%rsp), %rdi
leaq 0x188(%rsp), %rsi
callq 0x2270
leaq 0x2a8(%rsp), %rdi
callq 0x475c
leaq 0x2a8(%rsp), %rdi
callq 0x20f0
leaq 0x29fc(%rip), %rsi      # 0x501c
leaq 0x288(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x2ee0
leaq 0x70(%rsp), %rdi
leaq 0x288(%rsp), %rsi
callq 0x470e
leaq 0x288(%rsp), %rdi
callq 0x20f0
leaq 0x29c5(%rip), %rsi      # 0x5020
leaq 0x30(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x2ee0
leaq 0x29c7(%rip), %rsi      # 0x503b
leaq 0x70(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2691
leaq 0x30(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x2120
leaq 0x29a8(%rip), %rsi      # 0x5040
leaq 0x268(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x2ee0
leaq 0x110(%rsp), %rdi
leaq 0x268(%rsp), %rsi
callq 0x470e
leaq 0x268(%rsp), %rdi
callq 0x20f0
leaq 0x296e(%rip), %rsi      # 0x5044
leaq 0x50(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x2ee0
leaq 0x294c(%rip), %rsi      # 0x503b
leaq 0x110(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2712
leaq 0x50(%rsp), %rdi
leaq 0x110(%rsp), %rsi
callq 0x2120
leaq 0x130(%rsp), %rdi
callq 0x30b4
leaq 0x2934(%rip), %rsi      # 0x505a
leaq 0x228(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x2ee0
leaq 0x248(%rsp), %rdi
leaq 0x228(%rsp), %rsi
callq 0x470e
leaq 0x228(%rsp), %rdi
callq 0x20f0
leaq 0x28fd(%rip), %rsi      # 0x505e
leaq 0x10(%rsp), %rdi
leaq 0xf0(%rsp), %rdx
callq 0x2ee0
leaq 0x28c1(%rip), %rsi      # 0x503b
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x279a
leaq 0x10(%rsp), %rdi
leaq 0x248(%rsp), %rsi
callq 0x2120
leaq 0x28c6(%rip), %rsi      # 0x5067
leaq 0x208(%rsp), %rdi
leaq 0xd0(%rsp), %rdx
callq 0x2ee0
leaq 0xf0(%rsp), %rdi
leaq 0x208(%rsp), %rsi
callq 0x470e
leaq 0x208(%rsp), %rdi
callq 0x20f0
leaq 0x285c(%rip), %rsi      # 0x503b
leaq 0xf0(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2801
movq 0xf0(%rsp), %rdi
callq 0x22b0
movl %eax, %ebx
jmp 0x2804
pushq $0x7f
popq %rbx
leaq 0x2861(%rip), %rsi      # 0x506c
leaq 0x1e8(%rsp), %rdi
leaq 0xb0(%rsp), %rdx
callq 0x2ee0
leaq 0xd0(%rsp), %rdi
leaq 0x1e8(%rsp), %rsi
callq 0x470e
leaq 0x1e8(%rsp), %rdi
callq 0x20f0
leaq 0x27f2(%rip), %rsi      # 0x503b
leaq 0xd0(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x286d
movq 0xd0(%rsp), %rdi
callq 0x2210
cvtsd2ss %xmm0, %xmm0
jmp 0x2875
movss 0x278f(%rip), %xmm0     # 0x5004
movss %xmm0, 0x8(%rsp)
leaq 0x27ee(%rip), %rsi      # 0x5070
leaq 0x1c8(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x2ee0
leaq 0xb0(%rsp), %rdi
leaq 0x1c8(%rsp), %rsi
callq 0x470e
leaq 0x1c8(%rsp), %rdi
callq 0x20f0
leaq 0x277b(%rip), %rsi      # 0x503b
leaq 0xb0(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x28e4
movq 0xb0(%rsp), %rdi
callq 0x2210
cvtsd2ss %xmm0, %xmm0
jmp 0x28ec
movss 0x271c(%rip), %xmm0     # 0x5008
movss %xmm0, 0x4(%rsp)
leaq 0x277c(%rip), %rsi      # 0x5075
leaq 0x1a8(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x2ee0
leaq 0x90(%rsp), %rdi
leaq 0x1a8(%rsp), %rsi
callq 0x470e
leaq 0x1a8(%rsp), %rdi
callq 0x20f0
leaq 0x2707(%rip), %rsi      # 0x503b
leaq 0x90(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2958
movq 0x90(%rsp), %rdi
callq 0x2210
cvtsd2ss %xmm0, %xmm0
jmp 0x2960
movss 0x26ac(%rip), %xmm0     # 0x500c
movss %xmm0, 0xc(%rsp)
leaq 0x130(%rsp), %rdi
movss 0x269a(%rip), %xmm0     # 0x5010
callq 0x3018
leaq 0x26f8(%rip), %rsi      # 0x507a
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x29f7
leaq 0x26ec(%rip), %rsi      # 0x5083
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2a14
leaq 0x26b2(%rip), %rsi      # 0x505e
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2a2b
leaq 0x26ca(%rip), %rsi      # 0x508b
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2a42
leaq 0x26be(%rip), %rsi      # 0x5094
leaq 0x10(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
jne 0x2c8e
leaq 0x130(%rsp), %rdi
callq 0x3620
jmp 0x2a5d
leaq 0x130(%rsp), %rdi
movl %ebx, %esi
movss 0x4(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x3374
jmp 0x2a5d
leaq 0x130(%rsp), %rdi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
callq 0x31a4
jmp 0x2a5d
leaq 0x130(%rsp), %rdi
movl %ebx, %esi
movss 0x8(%rsp), %xmm0
callq 0x3288
jmp 0x2a5d
leaq 0x130(%rsp), %rdi
movl %ebx, %esi
movss 0x4(%rsp), %xmm0
movss 0xc(%rsp), %xmm1
callq 0x34c6
leaq 0x264c(%rip), %rsi      # 0x50b0
leaq 0x50(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2a95
movl $0x210, %edi            # imm = 0x210
callq 0x21d0
movq %rax, %r14
movq 0x50(%rsp), %rsi
pushq $0x14
popq %rdx
movq %rax, %rdi
callq 0x2260
addq $0x10, %r14
jmp 0x2a9c
movq 0x552c(%rip), %r14      # 0x7fc8
leaq 0x2614(%rip), %rsi      # 0x50b7
leaq 0x30(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2b00
movl $0x210, %edi            # imm = 0x210
callq 0x21d0
movq %rax, %r15
movq 0x30(%rsp), %rsi
pushq $0xc
popq %rdx
movq %rax, %rdi
callq 0x2260
movq %r15, %rdi
subq $-0x80, %rdi
callq 0x2090
testb %al, %al
jne 0x2b07
movq 0x54e3(%rip), %rdi      # 0x7fc8
leaq 0x25d1(%rip), %rsi      # 0x50bd
callq 0x21c0
movq %rax, %rdi
callq 0x2100
xorl %edi, %edi
callq 0x2200
movq 0x54d9(%rip), %r15      # 0x7fe0
movss 0x2505(%rip), %xmm0     # 0x5014
movss %xmm0, 0x4(%rsp)
leaq 0x3(%rsp), %rbx
pushq $0x1
popq %r12
leaq 0x130(%rsp), %r13
movq (%r15), %rax
movq -0x18(%rax), %rax
testb $0x2, 0x20(%r15,%rax)
jne 0x2ba6
movq %r15, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2220
movsbl 0x3(%rsp), %esi
movq %r13, %rdi
callq 0x30e6
testb %al, %al
je 0x2b26
movq %r13, %rdi
callq 0x3182
movaps %xmm0, %xmm2
cvttss2si %xmm0, %eax
movsbl %al, %eax
cvtsi2ss %eax, %xmm0
maxss 0x4(%rsp), %xmm2
movss 0x24a0(%rip), %xmm1     # 0x5018
movss %xmm2, 0x4(%rsp)
divss %xmm2, %xmm1
mulss %xmm0, %xmm1
cvttss2si %xmm1, %eax
movb %al, 0x3(%rsp)
movq %r14, %rdi
movq %rbx, %rsi
movq %r12, %rdx
callq 0x2240
movq %r14, %rdi
callq 0x2130
jmp 0x2b26
leaq 0x250a(%rip), %rsi      # 0x50b7
leaq 0x30(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2bc3
movq %r15, %rdi
callq 0x2050
leaq 0x24e6(%rip), %rsi      # 0x50b0
leaq 0x50(%rsp), %rdi
callq 0x20b0
testl %eax, %eax
je 0x2bea
leaq -0x10(%r14), %rdi
callq 0x2050
movq (%r14), %rax
movq %r14, %rdi
callq *0x8(%rax)
leaq 0x90(%rsp), %rdi
callq 0x20f0
leaq 0xb0(%rsp), %rdi
callq 0x20f0
leaq 0xd0(%rsp), %rdi
callq 0x20f0
leaq 0xf0(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x20f0
leaq 0x248(%rsp), %rdi
callq 0x20f0
leaq 0x130(%rsp), %rdi
callq 0x30ca
leaq 0x50(%rsp), %rdi
callq 0x20f0
leaq 0x110(%rsp), %rdi
callq 0x20f0
leaq 0x30(%rsp), %rdi
callq 0x20f0
leaq 0x70(%rsp), %rdi
callq 0x20f0
leaq 0x188(%rsp), %rdi
callq 0x20f0
xorl %eax, %eax
addq $0x2c8, %rsp            # imm = 0x2C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x5333(%rip), %rdi      # 0x7fc8
leaq 0x23fe(%rip), %rsi      # 0x509a
callq 0x21c0
leaq 0x10(%rsp), %rsi
movq %rax, %rdi
callq 0x21a0
movq %rax, %rdi
callq 0x2100
jmp 0x2af9
movq %rax, %rbx
movl $0x210, %esi            # imm = 0x210
movq %r15, %rdi
jmp 0x2cd3
movq %rax, %rbx
movl $0x210, %esi            # imm = 0x210
movq %r14, %rdi
callq 0x21e0
jmp 0x2de1
jmp 0x2dde
jmp 0x2d46
jmp 0x2d6e
jmp 0x2dcc
movq %rax, %rbx
leaq 0x1a8(%rsp), %rdi
jmp 0x2de9
movq %rax, %rbx
jmp 0x2dee
movq %rax, %rbx
leaq 0x1c8(%rsp), %rdi
jmp 0x2df6
movq %rax, %rbx
jmp 0x2dfb
movq %rax, %rbx
leaq 0x1e8(%rsp), %rdi
jmp 0x2e03
movq %rax, %rbx
jmp 0x2e08
movq %rax, %rbx
leaq 0x208(%rsp), %rdi
jmp 0x2e10
movq %rax, %rbx
jmp 0x2e15
movq %rax, %rbx
jmp 0x2e1f
movq %rax, %rbx
leaq 0x228(%rsp), %rdi
jmp 0x2e27
movq %rax, %rbx
jmp 0x2e2c
movq %rax, %rbx
jmp 0x2e39
movq %rax, %rbx
jmp 0x2e43
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
jmp 0x2e4b
jmp 0x2dcc
jmp 0x2dc4
movq %rax, %rbx
leaq 0x288(%rsp), %rdi
jmp 0x2e5f
jmp 0x2dbc
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
jmp 0x2e5f
jmp 0x2dbc
jmp 0x2dde
jmp 0x2dde
jmp 0x2dde
movq %rax, %rbx
jmp 0x2e64
movq %rax, %rbx
jmp 0x2e5a
movq %rax, %rbx
jmp 0x2e50
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
jmp 0x2e4b
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0x20f0
leaq 0xb0(%rsp), %rdi
callq 0x20f0
leaq 0xd0(%rsp), %rdi
callq 0x20f0
leaq 0xf0(%rsp), %rdi
callq 0x20f0
leaq 0x10(%rsp), %rdi
callq 0x20f0
leaq 0x248(%rsp), %rdi
callq 0x20f0
leaq 0x130(%rsp), %rdi
callq 0x30ca
leaq 0x50(%rsp), %rdi
callq 0x20f0
leaq 0x110(%rsp), %rdi
callq 0x20f0
leaq 0x30(%rsp), %rdi
callq 0x20f0
leaq 0x70(%rsp), %rdi
callq 0x20f0
leaq 0x188(%rsp), %rdi
callq 0x20f0
movq %rbx, %rdi
callq 0x2340