// File: /workspace/github_cpp_makefile_star2_O3/jkingan[P]ladyheather/heather.cpp
// Function: deglitch_plot_queue(int, double)
void deglitch_plot_queue(int pid, DATA_SIZE sigma)
{
long i;
long count;
long dg_count;
long pcount;
int id, start, end;
int col;
struct PLOT_Q q0, q;
DATA_SIZE sdev, avg;
DATA_SIZE val;

   // replace all queue points that are more than 3 standard deviations away
   // from the average value.  If pid < 0 all plots are deglitched, otherwise
   // only the specified plot is deglitched.


   if(queue_interval == 0) return;
   if(stat_count == 0) return;
   if(pid > (NUM_PLOTS+DERIVED_PLOTS)) return;

   if(pid < 0) {  // do all plots
      start = 0;
      end = (NUM_PLOTS+DERIVED_PLOTS-1);
if(debug_file) {
   fprintf(debug_file, "deglitch all plots: sigma:%g\n", sigma);
}
   }
   else {  // do single specified plot
      start = end = pid;
if(debug_file) {
   fprintf(debug_file, "deglitch plot %s: sigma:%g\n", plot[pid].plot_id,sigma);
}
   }

   count = 0;
   dg_count = 0;
   col = 0;
   i = plot_q_col0;   // deglitch displayed points
   pcount = plot_q_size;
////i = 0;            // deglitch all queue points
   while(pcount) {  // scan the data that is in the plot queue
      --pcount;
      if(i > 0) q0 = get_plot_q(i-1); // previous point
      else      q0 = get_plot_q(i);
      q = get_plot_q(i);              // point to deglitch

      for(id=start; id<=end; id++) {  // deglitch each plots' entries
         if(rcvr_type == CS_RCVR) ;        // don't deglitch message offset/jitter data
         else if(rcvr_type == TICC_RCVR) ;
         else if(id == NINE) continue;
         else if(id == TEN) continue;

         sdev = ((plot[id].sum_yy/stat_count) - ((plot[id].sum_y/stat_count)*(plot[id].sum_y/stat_count)) );
         if(sdev < (DATA_SIZE) 0.0) sdev = (DATA_SIZE) 0.0 - sdev;
         sdev = (DATA_SIZE) sqrt(sdev);

         avg = (DATA_SIZE) (plot[id].sum_y/stat_count);

         val = (DATA_SIZE) q.data[id]/(DATA_SIZE) queue_interval;

         if(val < (avg - (sdev*sigma))) {
            q.data[id] = q0.data[id];
            ++dg_count;
//sprintf(debug_text, "deglitch < %s:  #%ld  queue %ld   sdev:%g  val:%g  avg:%g", plot[id].plot_id, dg_count, i, sdev, val, avg);
if(debug_file) fprintf(debug_file, "deglitch < %s:  #%ld   queue %ld   sdev:%g  val:%g  avg:%g\n", plot[id].plot_id, dg_count, i, sdev, val, avg);
            put_plot_q(i, q);

         }
         else if(val > (avg + (sdev*sigma))) {
            q.data[id] = q0.data[id];
            ++dg_count;
//sprintf(debug_text, "deglitch > %s:  #%ld  queue %ld   sdev:%g  val:%g  avg:%g", plot[id].plot_id, dg_count, i, sdev, val, avg);
if(debug_file) fprintf(debug_file, "deglitch > %s:  #%ld   queue %ld   sdev:%g  val:%g  avg:%g\n", plot[id].plot_id, dg_count, i, sdev, val, avg);
            put_plot_q(i, q);
         }
      }

      ++i;
      if(i >= plot_q_size) i = 0;
      if(++count > plot_q_count) break;  // deglitch all points

      col += plot_mag;    // deglitch displayed points
      if(col >= (PLOT_WIDTH*view_interval)) break;
   }
// sprintf(debug_text3, "count:%ld  col:%d", count,col);
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1a8, %rsp            # imm = 0x1A8
movsd %xmm0, -0x40(%rbp)
movq %rdi, -0x38(%rbp)
cmpl $0x14, %edi
jg 0x10698
cmpq $0x0, 0x20afed(%rip)    # 0x21b398
je 0x10698
movsd 0x2fcbd7(%rip), %xmm0   # 0x30cf90
xorpd %xmm1, %xmm1
ucomisd %xmm1, %xmm0
jne 0x103c9
jnp 0x10698
movq 0x22dfc0(%rip), %rdi    # 0x23e390
cmpl $0x0, -0x38(%rbp)
js 0x1040b
testq %rdi, %rdi
je 0x10434
movq -0x38(%rbp), %rbx
movl %ebx, %eax
imulq $0xe8, %rax, %rax
leaq 0x14d941(%rip), %rcx    # 0x15dd30
movq (%rax,%rcx), %rdx
leaq 0xf9d3d(%rip), %rsi     # 0x10a137
movsd -0x40(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
movl %ebx, -0x2c(%rbp)
jmp 0x1043b
movl $0x13, -0x2c(%rbp)
movq $0x0, -0x38(%rbp)
testq %rdi, %rdi
je 0x1043b
leaq 0xf9cf3(%rip), %rsi     # 0x10a119
movsd -0x40(%rbp), %xmm0
movb $0x1, %al
callq 0x9560
jmp 0x1043b
movq -0x38(%rbp), %rax
movl %eax, -0x2c(%rbp)
movq 0x20aede(%rip), %rcx    # 0x21b320
testq %rcx, %rcx
je 0x10698
movq 0x20ae16(%rip), %r14    # 0x21b268
movq -0x38(%rbp), %rax
movl %eax, %edx
decl %eax
movl %eax, -0x44(%rbp)
movq %rdx, -0x60(%rbp)
imulq $0xe8, %rdx, %rax
leaq 0x14d8c1(%rip), %rdx    # 0x15dd30
addq %rax, %rdx
movq %rdx, -0x58(%rbp)
xorl %r15d, %r15d
movq $0x0, -0x50(%rbp)
xorl %esi, %esi
movq %rsi, -0x68(%rbp)
movq %rcx, -0x70(%rbp)
xorl %eax, %eax
testq %r14, %r14
setg %al
movq %r14, %rsi
subq %rax, %rsi
leaq -0x1d0(%rbp), %rdi
callq 0x100be
leaq -0x120(%rbp), %rdi
movq %r14, %rsi
callq 0x100be
movl -0x2c(%rbp), %eax
cmpl %eax, -0x38(%rbp)
ja 0x1063c
movq -0x58(%rbp), %r13
movl -0x44(%rbp), %r12d
movq -0x60(%rbp), %rbx
movq 0x2083fd(%rip), %rax    # 0x2188d0
cmpq $0x10000, %rax          # imm = 0x10000
je 0x104f3
cmpq $0x20000, %rax          # imm = 0x20000
je 0x104f3
movl %ebx, %eax
andl $0x7ffffffe, %eax       # imm = 0x7FFFFFFE
cmpl $0xc, %eax
je 0x10625
movsd 0x68(%r13), %xmm0
movsd 0x78(%r13), %xmm1
movsd 0x2fca89(%rip), %xmm2   # 0x30cf90
divsd %xmm2, %xmm1
divsd %xmm2, %xmm0
mulsd %xmm0, %xmm0
subsd %xmm0, %xmm1
xorpd %xmm2, %xmm2
subsd %xmm1, %xmm2
movapd %xmm1, %xmm0
xorpd %xmm3, %xmm3
cmpltsd %xmm3, %xmm0
andpd %xmm0, %xmm2
andnpd %xmm1, %xmm0
orpd %xmm2, %xmm0
ucomisd %xmm3, %xmm0
jb 0x10544
sqrtsd %xmm0, %xmm0
jmp 0x10549
callq 0x9270
movsd 0x68(%r13), %xmm2
divsd 0x2fca39(%rip), %xmm2   # 0x30cf90
xorps %xmm3, %xmm3
cvtsi2sdq 0x20ae35(%rip), %xmm3 # 0x21b398
movsd -0x110(%rbp,%rbx,8), %xmm1
divsd %xmm3, %xmm1
movapd %xmm0, %xmm3
mulsd -0x40(%rbp), %xmm3
movapd %xmm2, %xmm4
subsd %xmm3, %xmm4
ucomisd %xmm1, %xmm4
jbe 0x105b5
movsd -0x1c0(%rbp,%rbx,8), %xmm3
movsd %xmm3, -0x110(%rbp,%rbx,8)
incq %r15
movq 0x22dded(%rip), %rdi    # 0x23e390
testq %rdi, %rdi
je 0x105f8
movq (%r13), %rdx
leaq 0xf9ba0(%rip), %rsi     # 0x10a153
jmp 0x105eb
addsd %xmm2, %xmm3
ucomisd %xmm3, %xmm1
jbe 0x10625
movsd -0x1c0(%rbp,%rbx,8), %xmm3
movsd %xmm3, -0x110(%rbp,%rbx,8)
incq %r15
movq 0x22ddb5(%rip), %rdi    # 0x23e390
testq %rdi, %rdi
je 0x105f8
movq (%r13), %rdx
leaq 0xf9ba4(%rip), %rsi     # 0x10a18f
movq %r15, %rcx
movq %r14, %r8
movb $0x3, %al
callq 0x9560
testq %r14, %r14
js 0x10625
cmpq %r14, 0x20ad1c(%rip)    # 0x21b320
jle 0x10625
imulq $0xb0, %r14, %rdi
addq 0x20acfc(%rip), %rdi    # 0x21b310
movl $0xb0, %edx
leaq -0x120(%rbp), %rsi
callq 0x92b0
incq %rbx
incl %r12d
addq $0xe8, %r13
cmpl %r12d, -0x2c(%rbp)
jg 0x104cc
movq -0x68(%rbp), %rsi
cmpq 0x20acf1(%rip), %rsi    # 0x21b338
movq -0x70(%rbp), %rcx
jge 0x10698
incq %r14
cmpq 0x20acc9(%rip), %r14    # 0x21b320
movl $0x0, %eax
cmovgeq %rax, %r14
movslq 0x20ad69(%rip), %rax    # 0x21b3d0
movq -0x50(%rbp), %rdx
movslq %edx, %rdx
addq %rax, %rdx
movslq 0x20820c(%rip), %rax    # 0x218884
imulq 0x20ad20(%rip), %rax    # 0x21b3a0
movq %rdx, -0x50(%rbp)
cmpq %rdx, %rax
jle 0x10698
decq %rcx
incq %rsi
testq %rcx, %rcx
jne 0x10483
addq $0x1a8, %rsp            # imm = 0x1A8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq