// File: /workspace/github_c_makefile_star2_O0/DrYerzinia[P]C_DigiMode/src/packet/AFSK_Demodulator.c
// Function: AFSK_Demodulator_proccess_byte
char_array* AFSK_Demodulator_proccess_byte(AFSK_Demodulator *self, int8_t data_point){

	char_array* new_data = NULL;

	char_ring_buffer_put(&self->input_buffer, data_point);

	if(char_ring_buffer_avail(&self->input_buffer) > self->window){

		float q1_0 = 0;
		float q1_1 = 0;
		float q2_0 = 0;
		float q2_1 = 0;

		int16_t i;
		for(i = 0; i <= self->window; i++){

			float q0_0 = self->coeff0*q1_0 - q2_0 + char_ring_buffer_get(&self->input_buffer, i);
			float q0_1 = self->coeff1*q1_1 - q2_1 + char_ring_buffer_get(&self->input_buffer, i);
			q2_0 = q1_0;
			q2_1 = q1_1;
			q1_0 = q0_0;
			q1_1 = q0_1;

		}

		float fc1 = q1_0*q1_0 + q2_0*q2_0 - q1_0*q2_0*self->coeff0;
		float fc2 = q1_1*q1_1 + q2_1*q2_1 - q1_1*q2_1*self->coeff1;

		float fcd = fc1 - fc2;

		if(fcd > self->fcMax)
			self->fcMax = fcd*0.85 + self->fcMax*0.15;
		else
			self->fcMax = fcd*0.00015 + self->fcMax*0.99985;

		if(fcd < self->fcMin)
			self->fcMin = fcd*0.85 + self->fcMin*0.15;
		else
			self->fcMin = fcd*0.00015 + self->fcMin*0.99985;

		int generated_offset = (self->fcMax-self->fcMin)*self->offset;

		fcd -= generated_offset;

		char_ring_buffer_pop(&self->input_buffer);

		float_ring_buffer_put(&self->fcd_buffer, fcd);

		uint16_t avail = float_ring_buffer_avail(&self->fcd_buffer);
		if(avail > self->window/2){

			float fcd_avg = 0;
			for(i = 0; i < avail; i++)
				fcd_avg += float_ring_buffer_get(&self->fcd_buffer, i);
			fcd_avg /= avail;

			float_ring_buffer_pop(&self->fcd_buffer);

			/* Debug code to dump averaged Fourier coefficients to raw data
			 * file for analysis
			 */
			#ifdef __DEBUG
				fputc(fcd_avg, fourier_coefficient_debug);
			#endif

			uint8_t current_value = 0;
			if(fcd_avg < 0)
				current_value = 1;

			if(current_value != self->last_bit){

				self->last_bit = current_value;

				// Calculate how many bit lengths there are to the transition
				uint8_t new_bits = (int)((((float)self->count_last)/((float)self->bitwidth))+0.5);

				// If we are not bit stuffing Add a 0
				if(!self->bit_stuffing)
					char_ring_buffer_put(&self->bit_sequence, 0);

				// If we where bit stuffing stop now
				self->bit_stuffing = false;

				// Decrement new_bits
				new_bits--;

				// If new_bits > 5 we just found a preamble
				if(new_bits > 5){

					/* Mark the output waveform anytime we see a Preamble
					 */
					#ifdef __DEBUG
						fputc(127, fourier_coefficient_debug);
					#endif

					// Preamble related things

					uint16_t len = char_array_expandable_size(&self->byte_sequence);
					if(len >= 17){

						signed char *data = self->byte_sequence.data;
						new_data = (char_array*) malloc(sizeof(char_array));
						new_data->len = len;
						new_data->data = (signed char*) malloc(sizeof(char)*len);
						memcpy(new_data->data, data, len);

					}

					char_ring_buffer_clear(&self->bit_sequence);
					char_array_expandable_clear(&self->byte_sequence);

					// Set bit stuffing true so last bit of preamble will be removed
					self->bit_stuffing = true;

				}

				// If its not a preamble
				else {

					// If new_bits == 5 bit stuffing is occurring
					if(new_bits == 5)
						self->bit_stuffing = true;

					// Add the rest of the bits as 1's
					for(i = 0; i < new_bits; i++)
						char_ring_buffer_put(&self->bit_sequence, 1);

				}

				// Add bits to the sequence
				avail = char_ring_buffer_avail(&self->bit_sequence);
				if(avail >= 8){

					signed char byte = 0;
					for(i = 7; i >= 0; i--){
						byte <<= 1;
						if(char_ring_buffer_get(&self->bit_sequence, i)) byte |= 1;
					}

					char_ring_buffer_remove(&self->bit_sequence, 8);
					char_array_expandable_put(&self->byte_sequence, byte);

				}

				self->count_last = 0;

			} else
				self->count_last++;

		}

	}

	return new_data;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movb %sil, %al
movq %rdi, -0x8(%rbp)
movb %al, -0x9(%rbp)
movq $0x0, -0x18(%rbp)
movq -0x8(%rbp), %rdi
movsbl -0x9(%rbp), %esi
callq 0x16c0
movq -0x8(%rbp), %rdi
callq 0x1830
movzwl %ax, %eax
movq -0x8(%rbp), %rcx
movzbl 0x26(%rcx), %ecx
cmpl %ecx, %eax
jle 0x2483
xorps %xmm0, %xmm0
movss %xmm0, -0x1c(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x20(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x24(%rbp)
xorps %xmm0, %xmm0
movss %xmm0, -0x28(%rbp)
movw $0x0, -0x2a(%rbp)
movswl -0x2a(%rbp), %eax
movq -0x8(%rbp), %rcx
movzbl 0x26(%rcx), %ecx
cmpl %ecx, %eax
jg 0x1fd4
movq -0x8(%rbp), %rax
movss 0x2c(%rax), %xmm0
movss -0x1c(%rbp), %xmm2
movss -0x24(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x64(%rbp)
movq -0x8(%rbp), %rdi
movzwl -0x2a(%rbp), %esi
callq 0x1720
movss -0x64(%rbp), %xmm0
movsbl %al, %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
movss %xmm0, -0x30(%rbp)
movq -0x8(%rbp), %rax
movss 0x30(%rax), %xmm0
movss -0x20(%rbp), %xmm2
movss -0x28(%rbp), %xmm1
movd %xmm1, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm1
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x60(%rbp)
movq -0x8(%rbp), %rdi
movzwl -0x2a(%rbp), %esi
callq 0x1720
movss -0x60(%rbp), %xmm0
movsbl %al, %eax
cvtsi2ss %eax, %xmm1
addss %xmm1, %xmm0
movss %xmm0, -0x34(%rbp)
movss -0x1c(%rbp), %xmm0
movss %xmm0, -0x24(%rbp)
movss -0x20(%rbp), %xmm0
movss %xmm0, -0x28(%rbp)
movss -0x30(%rbp), %xmm0
movss %xmm0, -0x1c(%rbp)
movss -0x34(%rbp), %xmm0
movss %xmm0, -0x20(%rbp)
movw -0x2a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x1ee9
movss -0x1c(%rbp), %xmm1
movss -0x1c(%rbp), %xmm2
movss -0x24(%rbp), %xmm0
mulss -0x24(%rbp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss -0x1c(%rbp), %xmm0
mulss -0x24(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss 0x2c(%rax), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x38(%rbp)
movss -0x20(%rbp), %xmm1
movss -0x20(%rbp), %xmm2
movss -0x28(%rbp), %xmm0
mulss -0x28(%rbp), %xmm0
mulss %xmm2, %xmm1
addss %xmm0, %xmm1
movss -0x20(%rbp), %xmm0
mulss -0x28(%rbp), %xmm0
movq -0x8(%rbp), %rax
movss 0x30(%rax), %xmm2
movd %xmm0, %eax
xorl $0x80000000, %eax       # imm = 0x80000000
movd %eax, %xmm0
mulss %xmm2, %xmm0
addss %xmm1, %xmm0
movss %xmm0, -0x3c(%rbp)
movss -0x38(%rbp), %xmm0
subss -0x3c(%rbp), %xmm0
movss %xmm0, -0x40(%rbp)
movss -0x40(%rbp), %xmm0
movq -0x8(%rbp), %rax
cvtsi2ssl 0x50(%rax), %xmm1
ucomiss %xmm1, %xmm0
jbe 0x20c3
movss -0x40(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
cvtsi2sdl 0x50(%rax), %xmm1
movsd 0x1f96(%rip), %xmm2     # 0x4038
mulsd %xmm2, %xmm1
movsd 0x1f82(%rip), %xmm2     # 0x4030
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x50(%rax)
jmp 0x20fc
movss -0x40(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
cvtsi2sdl 0x50(%rax), %xmm1
movsd 0x1f4b(%rip), %xmm2     # 0x4028
mulsd %xmm2, %xmm1
movsd 0x1f37(%rip), %xmm2     # 0x4020
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x50(%rax)
movss -0x40(%rbp), %xmm1
movq -0x8(%rbp), %rax
cvtsi2ssl 0x54(%rax), %xmm0
ucomiss %xmm1, %xmm0
jbe 0x214a
movss -0x40(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
cvtsi2sdl 0x54(%rax), %xmm1
movsd 0x1f0f(%rip), %xmm2     # 0x4038
mulsd %xmm2, %xmm1
movsd 0x1efb(%rip), %xmm2     # 0x4030
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x54(%rax)
jmp 0x2183
movss -0x40(%rbp), %xmm0
cvtss2sd %xmm0, %xmm0
movq -0x8(%rbp), %rax
cvtsi2sdl 0x54(%rax), %xmm1
movsd 0x1ec4(%rip), %xmm2     # 0x4028
mulsd %xmm2, %xmm1
movsd 0x1eb0(%rip), %xmm2     # 0x4020
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %ecx
movq -0x8(%rbp), %rax
movl %ecx, 0x54(%rax)
movq -0x8(%rbp), %rax
movl 0x50(%rax), %eax
movq -0x8(%rbp), %rcx
subl 0x54(%rcx), %eax
cvtsi2ss %eax, %xmm0
movq -0x8(%rbp), %rax
mulss 0x34(%rax), %xmm0
cvttss2si %xmm0, %eax
movl %eax, -0x44(%rbp)
cvtsi2ssl -0x44(%rbp), %xmm1
movss -0x40(%rbp), %xmm0
subss %xmm1, %xmm0
movss %xmm0, -0x40(%rbp)
movq -0x8(%rbp), %rdi
callq 0x1780
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movss -0x40(%rbp), %xmm0
callq 0x1470
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
callq 0x15e0
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
movl %eax, -0x68(%rbp)
movq -0x8(%rbp), %rax
movzbl 0x26(%rax), %eax
movl $0x2, %ecx
cltd
idivl %ecx
movl %eax, %ecx
movl -0x68(%rbp), %eax
cmpl %ecx, %eax
jle 0x2481
xorps %xmm0, %xmm0
movss %xmm0, -0x4c(%rbp)
movw $0x0, -0x2a(%rbp)
movswl -0x2a(%rbp), %eax
movzwl -0x46(%rbp), %ecx
cmpl %ecx, %eax
jge 0x224b
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
movzwl -0x2a(%rbp), %esi
callq 0x14d0
addss -0x4c(%rbp), %xmm0
movss %xmm0, -0x4c(%rbp)
movw -0x2a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x2216
movzwl -0x46(%rbp), %eax
cvtsi2ss %eax, %xmm1
movss -0x4c(%rbp), %xmm0
divss %xmm1, %xmm0
movss %xmm0, -0x4c(%rbp)
movq -0x8(%rbp), %rdi
addq $0x10, %rdi
callq 0x1530
movb $0x0, -0x4d(%rbp)
xorps %xmm0, %xmm0
ucomiss -0x4c(%rbp), %xmm0
jbe 0x227f
movb $0x1, -0x4d(%rbp)
movzbl -0x4d(%rbp), %eax
movq -0x8(%rbp), %rcx
movzbl 0x58(%rcx), %ecx
cmpl %ecx, %eax
je 0x246f
movb -0x4d(%rbp), %cl
movq -0x8(%rbp), %rax
movb %cl, 0x58(%rax)
movq -0x8(%rbp), %rax
movzwl 0x3c(%rax), %ecx
cvtsi2ss %ecx, %xmm0
movzbl 0x3e(%rax), %eax
cvtsi2ss %eax, %xmm1
divss %xmm1, %xmm0
cvtss2sd %xmm0, %xmm0
movsd 0x1d57(%rip), %xmm1     # 0x4018
addsd %xmm1, %xmm0
cvttsd2si %xmm0, %eax
movb %al, -0x4e(%rbp)
movq -0x8(%rbp), %rax
testb $0x1, 0x59(%rax)
jne 0x22e5
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
xorl %esi, %esi
callq 0x16c0
movq -0x8(%rbp), %rax
movb $0x0, 0x59(%rax)
movb -0x4e(%rbp), %al
addb $-0x1, %al
movb %al, -0x4e(%rbp)
movzbl -0x4e(%rbp), %eax
cmpl $0x5, %eax
jle 0x2396
movq -0x8(%rbp), %rdi
addq $0x60, %rdi
callq 0x13b0
movw %ax, -0x50(%rbp)
movzwl -0x50(%rbp), %eax
cmpl $0x11, %eax
jl 0x2372
movq -0x8(%rbp), %rax
movq 0x68(%rax), %rax
movq %rax, -0x58(%rbp)
movl $0x10, %edi
callq 0x10f0
movq %rax, -0x18(%rbp)
movw -0x50(%rbp), %cx
movq -0x18(%rbp), %rax
movw %cx, (%rax)
movzwl -0x50(%rbp), %eax
movl %eax, %edi
shlq $0x0, %rdi
callq 0x10f0
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x58(%rbp), %rsi
movzwl -0x50(%rbp), %eax
movl %eax, %edx
callq 0x10e0
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
callq 0x1810
movq -0x8(%rbp), %rdi
addq $0x60, %rdi
callq 0x13d0
movq -0x8(%rbp), %rax
movb $0x1, 0x59(%rax)
jmp 0x23db
movzbl -0x4e(%rbp), %eax
cmpl $0x5, %eax
jne 0x23a7
movq -0x8(%rbp), %rax
movb $0x1, 0x59(%rax)
movw $0x0, -0x2a(%rbp)
movswl -0x2a(%rbp), %eax
movzbl -0x4e(%rbp), %ecx
cmpl %ecx, %eax
jge 0x23d9
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movl $0x1, %esi
callq 0x16c0
movw -0x2a(%rbp), %ax
addw $0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x23ad
jmp 0x23db
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
callq 0x1830
movw %ax, -0x46(%rbp)
movzwl -0x46(%rbp), %eax
cmpl $0x8, %eax
jl 0x2463
movb $0x0, -0x59(%rbp)
movw $0x7, -0x2a(%rbp)
movswl -0x2a(%rbp), %eax
cmpl $0x0, %eax
jl 0x2440
movsbl -0x59(%rbp), %eax
shll %eax
movb %al, -0x59(%rbp)
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movzwl -0x2a(%rbp), %esi
callq 0x1720
cmpb $0x0, %al
je 0x2430
movsbl -0x59(%rbp), %eax
orl $0x1, %eax
movb %al, -0x59(%rbp)
jmp 0x2432
movw -0x2a(%rbp), %ax
addw $-0x1, %ax
movw %ax, -0x2a(%rbp)
jmp 0x23ff
movq -0x8(%rbp), %rdi
addq $0x40, %rdi
movl $0x8, %esi
callq 0x17c0
movq -0x8(%rbp), %rdi
addq $0x60, %rdi
movsbl -0x59(%rbp), %esi
callq 0x12c0
movq -0x8(%rbp), %rax
movw $0x0, 0x3c(%rax)
jmp 0x247f
movq -0x8(%rbp), %rax
movw 0x3c(%rax), %cx
addw $0x1, %cx
movw %cx, 0x3c(%rax)
jmp 0x2481
jmp 0x2483
movq -0x18(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
nopl (%rax)