//  fast calculation of pitch of sound wave

 

 

//   ***********************************************

//   *****     list of functions-libraries     *****

//   ***********************************************

 

 

 

#include <fstream.h>

#include <iostream.h>

#include <strstream.h>

#include <stdio.h>

#include <graphics.h>

#include <stdlib.h>

#include <conio.h>

#include <constrea.h>

#include <iomanip.h>

#include <string.h>

#include <dir.h>

#include <ctype.h>

#include <alloc.h>

#include <math.h>

#include <process.h>

 

 

//   ************************************

//   *****     global functions     *****

//   ************************************

//int main(void);

int maxminx[100], maxminy[100];       //all values of max and min points on screen

int maxminax[100], maxminay[100];       //all values of alternative max and min points on screen

int maxmink[100], maxmind[100];

int maxminxl[100], maxminyl[100];       //all values of max and min points on screen

int maxminkl[100], maxmindl[100];

int maxmind2[100];

int wvrepx[100], wvrepy[100];         //values of high similarity between waves circles

int wvrepe[100];         //values of high similarity between waves circles

int wvrepxl[100], wvrepyl[100];         //values of low similarity between waves circles

float result[5][1001];         //values of results

int result_no=0;

int result_end;

//float avg_multip, avg_no;

int pcounter, pcount2, pcount3, pcount4; //counter of these points

int calcirx, calciry;               //calculated min & max circle centers

int pfound;               //temporary for debug

int calcirx2, calciry2;               //calculated min & max circle centers

int calcirx3, calciry3;               //calculated min & max circle centers

int calcirx4, calciry4;               //calculated min & max circle centers

void calc_coords(void);             //calculations of global variables

double y_to_pix();

double x_to_pix();

int a_num1=0, b_num1=0, a_num2=0, b_num2=0;

 

// Draw one line

void circle(int centerx, int centery, int radius, int color);

void draw_line(int pos_x0, int pos_y0, int pos_x1, int pos_y1, int color);

void draw_line2(int pos2_x0, int pos2_y0, int pos2_x1, int pos2_y1, int color2);

void get_key(void);         // Display text on graphics screen,

// Wait for key

 

 

//   =====     global variables     =====

 

long double strt_po = 43, cur_po = 43, prev_stp=0; //start place in the file

long volume = 0, volume2 = 0;

long double skip_num; //value for consecutive jumps of screen

 

// parameters for values to pixels

double x_no_min = 0, y_no_min = (-32768);

double x_no_max = 1600, y_no_max = 32767;//min and max values in file

 

double x_gra_min, y_gra_min;

double x_gra_max, y_gra_max;          //min and max pixels of graph

double x_scr_max, y_scr_max;          //max pixels in screen

int highsim=0;                        //signifies if high similarity was found

int crosing=0;                       //signifies if crossing of centre was done after 3rd maxmin-circle

int first_c=2;                        //signifies first circle to start repeatness check from

int first_c3;

int key=0;                            //key that was pressed

int startr;

int a;

 

fstream f1;

int color;                            //color of graphline

char fi_name1[40] = "";

double scal_x=0, scal_y=0;               //% deviation from 100% scaling

int sc_x_in, sc_y_in;                 //% deviation from 100% scaling - input

char y_input, y_input2;               //character  for input

int y1, y2, y1_prev;                  // int values of input characters

double y_get, y_prev1, y_prev2;       //current values of input-points

double y_cir1l, y_cir2l, x_cir2l;

double y_cir3l, x_cir3l, y_cir4l, x_cir4l, x_cir1l;

double y_cir1, y_cir2, x_cir2;

double y_cir3, x_cir3, y_cir4;

double x_cir4, x_try, y_cir_prev=(-999);

double x_cir_prev=(-999);

double x_get, x_prev1, x_cir1, x_cir1b;        //   "       "   "     "    "

double y_pix, x_pix;          //current values of pixels

double y_var1, y_var2, y_var3;

double y_pix_pre, x_pix_pre ;         //previous values of pixels

double x_pix_q_st;                    //start of quite line

double q_line_l, q_line_t;            //quite line length in points and q_line_temporary(=local)

int quite_f;                          //signifies if quite was drawn

double y_prev_vol,y_cur_vol;          //values of volume (avarage)

double y_pre_v2,y_cur_v2;             //values of volume (maximal)

double y_min_hi = 0,y_max_hi = 0,y_high = 0;     //values of volume

int abssqrt=0;                        //temporary absolute value for square root calculations

int endcount;

int flag2=0,pnum=0;                   //checks for similarity circles before

double fraction, integer;

float prnt1,prnt2;

float sim_kind;

 

int startrun = 0;

int tip_line=1;

int flag=0;                          //flag if a max or min circle was drawn

int quite=0;

float add0;

 

FILE *stream;

char msg[] = "this is a test";

char buf[20];

int numrun=0;

double endrun=0;

int points_ca = 0;           //number of points canceled

int points_ca2 = 0;           //number of points canceled

int points_ca3 = 0;           //number of points canceled

int points_ca4 = 0;           //number of points canceled

int points = 0;           //number of points checked

int cir_can=0, cir_can2=0, cir_can3=0, cir_can4=0, cir_all=0, cir_canm=0, cir_canno=0;

int cir_dis=0, cir_dis2=0, cir_dis3=0, cir_dis4=0, cir_candi=0;

//accumalator of points canceled and checked

float dist_s = 0;

float dist_s2 = 0;

int formant1x = 0;            //x value of highest point in returning value

int formant1xp = 0;            //x value of highest point in returning value

int formant1yp = 0;            //x value of highest point in returning value

int formant1xn = 0;            //x value of highest point in returning value

int formant2x = 0;            //x value of highest point in returning value

int formant2xp = 0;            //x value of highest point in returning value

int formant2xn = 0;            //x value of highest point in returning value

int formant1y = 0;            //x value of highest point in returning value

int formant2y = 0;            //x value of highest point in returning value

//int graphdriver = VGA, graphmode;

 

 

void main(int argc, char *argv[])

//void main()

{

//fi_name1[40] = argv[1];

 

//initgraph(&graphdriver, &graphmode, "c:\\tc\\bgi");

calc_coords();

//cleardevice;                        //erases the screen

 

 

          //opening new files

//stream = fopen("wavedat.TXT", "w+");

 

stream = fopen(argv[2], "w+");

 

  for (result_no=1;result_no<1000;result_no++) {

  result[0][result_no]=0;

  result[1][result_no]=0;

  result[2][result_no]=0;

  result[3][result_no]=0;

  result[4][result_no]=0;

  }

result_no=0;

 

 

 f1.open(argv[1], ios::in|ios::binary);

 //f1.open("C:\\tc\\song.wav", ios::in|ios::binary);

 if (!f1)

  {

  cout << "cannot open file " << fi_name1;

  strt_po = -(1);

  }

f1.seekg(1,ios::end);

endrun = f1.tellg() - 3;

//cout << endrun;

while (strt_po != -(1))             // loop while not Esc

 {

 //giving start value to array

 //********************************************************

 for (pcounter=0;pcounter<100;pcounter++) {

  maxminx[pcounter] = 0;

  maxminy[pcounter] = 0;

  maxminax[pcounter] = 0;

  maxminay[pcounter] = 0;

  maxmink[pcounter] = 0;

  maxminxl[pcounter] = 0;

  maxminyl[pcounter] = 0;

  maxminkl[pcounter] = 0;

  maxmind[pcounter] = 0;

  maxmind2[pcounter] = 0;

  wvrepx[pcounter] = 0;

  wvrepe[pcounter] = 0;

  wvrepy[pcounter] = 0;

  wvrepxl[pcounter] = 0;

  wvrepyl[pcounter] = 0;

 

  }

// for (pcounter=0;pcounter<1600;pcounter++) {

//  ypixl[pcounter] = 0;

//  ypixh[pcounter] = 0;

//  }

 pcounter=0;

 y_cir1=0;

 y_cir2=0;

 y_cir3=0;

 y_cir4=0;

 x_cir1=0;

 x_cir2=0;

 x_cir3=0;

 x_cir4=0;

 y_cir_prev=(-999);

 x_cir_prev=(-999);

volume = 0;

 volume2 = 0;

 q_line_l = 0;

 

 scal_x = 0;

x_get = 0;

 cur_po = strt_po;

 

 if (numrun!=0) {

   strt_po=strt_po+skip_num;

 }

 if (numrun==0) {

   strt_po=1001;

   //cin >> scal_y;

   skip_num=1000;

   //cin >> endrun;

 }

 numrun=numrun+1;

 cur_po = strt_po;

//endrun = 200000;

//cout <<cur_po;

 f1.seekg(cur_po,ios::beg);

 x_get = 0;

 y_get = 0;

 y_prev1 = y_get;

 x_prev1 = x_get;

 

 y_pix_pre = (y_gra_max - y_gra_min) / 2 + y_gra_min;

 x_pix_pre = x_gra_min;

 first_c=2;

 startrun = 0;

 quite_f=0;

 y_min_hi=0;

 y_max_hi=0;

 

 

 

 

  while (x_get <= x_no_max)

   {

f1.get(y_input);

   y1 = int(y_input);

 

f1.get(y_input2);

   y2 = int(y_input2);

y_get = (y1 + 1) * 256 + y2 + 1;

 

   y_to_pix();             //translate to screen values

 

   x_to_pix();             //translate to screen values

 

 

 

//draw volume-line 2 (maximal)

  if ((y_min_hi > y_get) && (y_get < 0)) y_min_hi=y_get;

  if ((y_max_hi < y_get) && (y_get >0)) y_max_hi=y_get;

 

//end volume-line 2

     x_get++;

     x_get++;

     }

y_high = 0;

if (abs(y_min_hi) > abs(y_max_hi)) y_high = abs(y_min_hi);

  else y_high = abs(y_max_hi);

 

if (y_high !=0) scal_y = y_no_max / y_high * 80 / 100;

  else scal_y = 0;

 

 

f1.seekg(cur_po,ios::beg);

 x_get = 0;

 y_get = 0;

 

 

while (x_get <= x_no_max)

   {

f1.get(y_input);

   y1 = int(y_input);

//   cur_po++;

 

f1.get(y_input2);

   y2 = int(y_input2);

//   cur_po++;

y_get = (y1 + 1) * 256 + y2 + 1;

 

   y_to_pix();             //translate to screen values

 

   x_to_pix();             //translate to screen values

 

 

 

 

 

 

   startrun = 1;

 

 

   //draw max-circle

   if ((y_prev1 > y_pix) && (y_prev1 > y_prev2) && tip_line==1) {

    flag=1;

    tip_line=2;

   }

 

   //draw min-circle

    if ((y_prev1 < y_pix) && (y_prev1 < y_prev2) && tip_line==2) {

     flag=2;

     tip_line=1;

    }

 

    //draw max-circle_2

    if ((y_prev1 > y_pix) && (y_prev1 == y_prev2) && tip_line==1) {

     tip_line=2;

     flag=1;

    }

    if ((y_prev1 == y_pix) && (y_prev1 > y_prev2)) tip_line=1;

 

    //draw min-circle_2

    if ((y_prev1 < y_pix) && (y_prev1 == y_prev2) && tip_line==2) {

     tip_line=1;

     flag=2;

    }

    if ((y_prev1 == y_pix) && (y_prev1 < y_prev2)) tip_line=2;

 

    //alternative values if there is problematic recognition

    //****if ((y_prev1 > maxminy[pcounter]) && (flag==1)) {maxminay[pcounter]= y_prev1; maxminax[pcounter]= x_prev1}

    //****if ((y_prev1 < maxminy[pcounter]) && (flag==2)) {maxminay[pcounter]= y_prev1; maxminax[pcounter]= x_prev1}

 

    //erase middle-circle  //if two circles max then min are close and touch - erase both

    //*

    //*

    //*           fix so that checks if corespondence between red and next red and not red and yellow

    if (((y_cir1 - y_cir2)*(y_cir1 - y_cir2)+(x_cir1 - x_cir2)*(x_cir1 - x_ccir2)) <= 128)

     {

     if (((y_cir3 <= y_cir2) && (y_cir2 <= y_prev1)) || y_cir1==y_cir3)

      {

      if (flag==1)

       {

       y_cir1 = y_cir3;

       y_cir2 = y_cir4;

       y_cir4 = 0;

       y_cir3 = 0;

       x_cir1 = x_cir3;

       x_cir2 = x_cir4;

       x_cir4 = 0;

       x_cir3 = 0;

       }

      }

     }

 

     if (((y_cir1 - y_cir2)*(y_cir1 - y_cir2)+(x_cir1 - x_cir2)*(x_cir1 - x_ccir2)) <= 128)

      {

      if (((y_cir3 >= y_cir2) && (y_cir2 >= y_prev1)) || y_cir1==y_cir3)

       {

       if (flag==2)

      {

      y_cir1 = y_cir3;

      y_cir2 = y_cir4;

      y_cir4 = 0;

      y_cir3 = 0;

      x_cir1 = x_cir3;

      x_cir2 = x_cir4;

      x_cir4 = 0;

      x_cir3 = 0;

      }

       }

      }

 

 

 

 

     if (flag != 0)

      {

      //cluster on the y_scale - solution to this problem

      //posibble to run also on diffrence in x_y together (pitagoras).

      if (((y_cir_prev - y_cir4)*(y_cir_prev - y_cir4)+(x_cir_prev - x_cir4)*(x_cir_prrev - x_cir4)) >= 128) {

      /////

 

      //**********************************************************8

      if (x_cir4 * y_cir4 !=0)

       {

       if (pcounter<100) pcounter=pcounter+1;

       maxminx[pcounter] = x_cir4;

       maxminy[pcounter] = y_cir4;

       maxmink[pcounter] = flag;

       }

      /////

      if (x_cir4 != 0) {

       y_cir_prev = y_cir4;

       x_cir_prev = x_cir4;

       }

      }

      y_cir4 = y_cir3;

      y_cir3 = y_cir2;

      y_cir2 = y_cir1;

      y_cir1 = y_prev1;

      x_cir4 = x_cir3;

      x_cir3 = x_cir2;

      x_cir2 = x_cir1;

      x_cir1 = x_prev1;

 

      }

flag=0;

     ///////end erase

 

     //draw quite line

     if ((y_prev1-((y_gra_max - y_gra_min) / 2 + y_gra_min) < 8) && (y_prev1-((y_gra_max - y_gra_min) / 2 + y_gra_min) > -(8)))

      {

      if (quite == 0)

       {

       x_pix_q_st=x_prev1;

       quite=1;

       q_line_t = 0;

       }

      if (quite == 1)

       {

       ///a length of four pairs of circles of quite will result a quite to be drawn

       if (x_prev1-x_pix_q_st >= 64) {            //four pairs of circles, min and max=minimal length for quite

      quite_f = 1;

      q_line_l = q_line_l + (x_prev1-x_pix_q_st) - q_line_t;

      q_line_t = x_prev1-x_pix_q_st;

      }

       }

      }

      else {

       quite=0;

      }

//end quite line

//draw volume-line 1 (avarage)

  volume = volume+abs(y_get);

  if (x_get != 0) {

  y_cur_vol = y_gra_max - (((volume / (x_get / 2)) - y_no_min) * (y_gra_max - y_gra_min) / (y_no_max - y_no_min) * scal_y);

 

y_var1 = y_gra_max - (((volume / (x_get / 2)) - y_no_min) * (y_gra_max - y_gra_min) / (y_no_max - y_no_min));

y_var2 = (y_gra_max - y_gra_min) / 2 + y_gra_min;

y_cur_vol = (y_var1 - y_var2) * scal_y + y_var2;

 

y_cur_vol=0;

  }

  else {

  y_cur_vol = y_gra_max - ((0 - y_no_min) * (y_gra_max - y_gra_min) / (y_no_max - y_no_min) * scal_y);

y_var1 = y_gra_max - ((0 - y_no_min) * (y_gra_max - y_gra_min) / (y_no_max - y_no_min));

y_var2 = (y_gra_max - y_gra_min) / 2 + y_gra_min;

y_cur_vol = (y_var1 - y_var2) * scal_y + y_var2;

 

  }

 

  y_prev_vol = y_cur_vol;

//end volume-line

 

 

 

     y_prev2 = y_prev1;

     y_prev1 = y_pix;

     x_prev1 = x_pix;

     y_pix_pre = y_pix;

     x_pix_pre = x_pix;

     x_get++;

     x_get++;

     }

//   f1.close();

 

///Erasing minimal values circles and unifing maximal when over-volume occurs

 

       for (pcount2=1;pcount2<49;pcount2++) {

       //if (strt_po == 365001) cout << pcount2 << " " << maxminy[pcount2] << " ; ";

         if ((maxminx[pcount2+1] - maxminx[pcount2] < 3) && (maxminy[pcount2] >= 162) && (maxmink[pcount2+1] == 2)) {

           for (pcount3=pcount2;pcount3>=1;pcount3--) {

             if (maxminy[pcount3] >= 162) {

             maxminx[pcount3] = 0;

             maxminy[pcount3] = 0;

             maxmink[pcount3] = 0;

             }

             if ((maxminy[pcount3] < 162) && (maxmink[pcount3] == 2)) {

             maxminx[pcount3] = (maxminx[pcount2+1]+maxminx[pcount3]) / 2;

             maxminy[pcount3] = (maxminy[pcount2+1]+maxminy[pcount3]) / 2;

             maxmink[pcount3] = 2;

             maxminy[pcount2+1] = 0;

             maxminx[pcount2+1] = 0;

             maxmink[pcount2+1] = 0;

             break;

             }

           }

         }

       }

       //removing the 0 values

       //if (strt_po == 365001) cout << first_c << " " << pcounter+1 << " limits; ";

       pcount3=0;

       for (pcount2=1;pcount2<100;pcount2++) {

         //if ((maxmink[pcount2] == 0) && (maxminx[pcount2] != 0) && (pcount2 <= pcounter)) pcounter--;

         if (maxmink[pcount2] != 0) {

           pcount3++;

           maxminx[pcount3] = maxminx[pcount2];

           maxminy[pcount3] = maxminy[pcount2];

           maxmink[pcount3] = maxmink[pcount2];

           //if (strt_po == 365001) cout << pcount3 << " " << maxminx[pcount3] << " " << maxminy[pcount3] << " ; ";

         }

       }

       //if (strt_po == 365001) cout << first_c << " " << pcounter+1 << " limits; ";

       //if (strt_po == 365001) getch();

 

/////*********************

/////start of second run : checking similarity between waves

////after the line was drawn fully and max min points marked

 

  ////run repeatness calculations only if quite line was drawn

  ////PREVIOUSrun repeatness calculations only if quite line width is smaller than 80% of screen width

//TRYTRY    if (quite_f == 0) {

  if (quite_f != 15) {

  //first_c=2;

  //y_var2 = (y_gra_max - y_gra_min) / 2 + y_gra_min;

  //if (maxmink[2] == maxmink[1]) first_c=3;

  //if (maxmink[2] == maxmink[3]) first_c=3;

 

 

  //prelimenary run to calculate number of min/max circles

  //***

  endcount = 100;

  for (pcounter=first_c;pcounter<100;pcounter++) {

  if ((maxminx[pcounter] == 0) && (maxminy[pcounter] == 0)) {

  endcount = pcounter-1;

  break;

  }

  }

 

  highsim = 0;                 //checks if similarity was found

  int sdistance = 0;           //sum of distances from the line

 

  //For Printing Debuging

//  if (strt_po==188001) {

//for (pcounter=first_c;pcounter<=endcount;pcounter++) {   ///starts at the third point (not the first) ends at 2/3 of the number of points

//fprintf(stream, "%6i%6i%6i%6i\n",

//int(maxminx[pcounter]),

//int(maxminy[pcounter]),

//int(maxminx[pcounter])-int(maxminx[pcounter-1]),

//int(maxminy[pcounter])-int(maxminy[pcounter-1]));

 

//  cout << int(maxminx[pcounter]) << " " << int(maxminy[pcounter]) << " " << int(maxminx[pcounter])-int(maxminx[pcounter-1]) << " " << int(maxminy[pcounter])-int(maxminy[pcounter-1]) << '\n';

//}

//}

 

crosing=0;

//finding if crossing of middle line was done after third circle

for (pcount4=first_c;pcount4<=endcount*2 / 3;pcount4++) {

 

if ((crosing==0) && (maxminy[first_c] >= y_var2) && (maxminy[pcount4]<=y_var2)) crosing=1;

if ((crosing==0) && (maxminy[first_c] <= y_var2) && (maxminy[pcount4]>=y_var2)) crosing=1;

if ((crosing==1) && (maxminy[first_c] >= y_var2) && (maxminy[pcount4]>=y_var2)) break;

if ((crosing==1) && (maxminy[first_c] <= y_var2) && (maxminy[pcount4]<=y_var2)) break;

//if ((strt_po == 34001) && (crosing==2)) cout << pcount2 << " " << first_c << '\n';

}

 

//run of outer array

for (pcounter=pcount4;pcounter<=endcount*2 / 3;pcounter++) {   ///starts at the third point (not the first) ends at 2/3 of the number of points

 

 

  //variables

  int checkp=0;                //checkpoint if empty or full

  int pcolor=0;                //color of point

  float cpoint;                //check point y value from circle center

  float cpointx, cpointy;      //check points

  int radius = 10;             //current check-circle radius

  int radius1 = 0;             //largest empty radius

  int radius2 = 20;            //smallest full radius

  sdistance = 0;           //sum of distances from the line

  float dist_x = 0;

  float dist_y = 0;

  float dist_2 = 0;

  float dist = 0;

  float dist_x2 = 0;

  float dist_y2 = 0;

  float dist_22 = 0;

  float dist2 = 0;

  float dist_x3 = 0;

  float dist_y3 = 0;

  float dist_33 = 0;

  float dist3 = 0;

  float dist_x4 = 0;

  float dist_y4 = 0;

  float dist_44 = 0;

  float dist4 = 0;

  cir_all = 0;

  cir_canm = 0;

  cir_canno = 0;

  cir_candi = 0;

  cir_can = 0;

  cir_can2 = 0;

  cir_can3 = 0;

  cir_can4 = 0;

  cir_dis = 0;

  cir_dis2 = 0;

  cir_dis3 = 0;

  cir_dis4 = 0;

  dist_s = 0;

  dist_s2 = 0;

  a_num1=0, b_num1=0, a_num2=0, b_num2=0;

 

     calcirx = maxminx[pcounter] - maxminx[first_c];              //calculated circle centers distance

     a_num1=pcounter-first_c;

     b_num2=endcount-pcounter+1;

     calciry = 0;

     pfound=0;

//from second start point

     calcirx2 = maxminx[pcounter] - maxminx[first_c+1];              //calculated circle centers distance

     calciry2 = 0;

 

     calcirx3 = maxminx[pcounter+1] - maxminx[first_c+1];              //calculated circle centers distance

     calciry3 = 0;

 

     calcirx4 = maxminx[pcounter+1] - maxminx[first_c];              //calculated circle centers distance

     calciry4 = 0;

//if (strt_po==188001) cout << ";";

//if (strt_po==217001) cout << int(calcirx) << ";";

 

     first_c3=first_c;

  //run of inner arrey // on all cir-points

  //***

    for (pcount2=pcounter;pcount2<=endcount;pcount2++) {     //starts at the next point after the first array

 

  //if (strt_po==xxx001) cout << int(maxminx[pcount2]) << " " << int(maxminy[pcount2]) << " ";

 

    ///run for calculation of calc-circle-center from closest line

    ///**

    ///NEW calculation of similarity between circles

   points_ca = 0;

   points_ca2 = 0;

   points_ca3 = 0;

   points_ca4 = 0;

   maxmind[pcount2]=0;

   maxmind2[pcount2]=0;

 

//-----------------------------------------------

for (pcount3=first_c3;pcount3<=pcount2-1;pcount3++) {

   dist_2=999;

   dist_22=999;

   dist_33=999;

   dist_44=999;

  //if (maxmink[pcount2]==maxmink[pcount3]) {

  dist_x = (maxminx[pcount2]-calcirx) - maxminx[pcount3];

  //dist = sqrt(dist_2);

 

  dist_x4 = (maxminx[pcount2]-calcirx4) - maxminx[pcount3];

  //dist4 = sqrt(dist_44);

//if ((strt_po==217001)) cout << first_c3 << " ";

  //}

//from second start point

  //if (pcount2>pcounter){

  if (pcounter>first_c+1){

  //if (maxmink[pcount2]==maxmink[pcount3]) {

  dist_x2 = (maxminx[pcount2]-calcirx2) - maxminx[pcount3];

  dist_x3 = (maxminx[pcount2]-calcirx3) - maxminx[pcount3];

  if ((dist_x < -(11)) && (dist_x2 < -(11)) && (dist_x3 < -(11)) && (dist_x4 < -(11))) break;

  if ((dist_x > (11)) && (dist_x2 > (11)) && (dist_x3 > (11)) && (dist_x4 > (11))) {first_c3=pcount3; continue;}

 

  dist_y2 = maxminy[pcount2] - maxminy[pcount3];

  dist_22 = (dist_x2 * dist_x2) + (dist_y2 * dist_y2);

if (maxmink[pcount2] != maxmink[pcount3]) dist_22=999;

  //dist2 = sqrt(dist_22);

 

  dist_y3 = maxminy[pcount2] - maxminy[pcount3];

  dist_33 = (dist_x3 * dist_x3) + (dist_y3 * dist_y3);

if (maxmink[pcount2] != maxmink[pcount3]) dist_33=999;

  //dist3 = sqrt(dist_33);

  }

  else

  {

  if ((dist_x < -(11)) && (dist_x4 < -(11))) break;

  if ((dist_x > (11)) && (dist_x4 > (11))) {first_c3=pcount3; continue;}

 

  }

  dist_y = maxminy[pcount2] - maxminy[pcount3];

  dist_2 = (dist_x * dist_x) + (dist_y * dist_y);

if (maxmink[pcount2] != maxmink[pcount3]) dist_2=999;

  dist_y4 = maxminy[pcount2] - maxminy[pcount3];

  dist_44 = (dist_x4 * dist_x4) + (dist_y4 * dist_y4);

if (maxmink[pcount2] != maxmink[pcount3]) dist_44=999;

 

  //}

  //

  if (dist_2 <= 121) {points_ca = 1;}//;cir_dis = cir_dis + dist_x;}

  if (dist_22 <= 121) {points_ca2 = 1;}//;cir_dis2 = cir_dis2 + dist_x2;}

  if (dist_33 <= 121) {points_ca3 = 1;}//;cir_dis3 = cir_dis3 + dist_x3;}

  if (dist_44 <= 121) {points_ca4 = 1;}//;cir_dis4 = cir_dis4 + dist_x4;}

}

 

cir_can = cir_can + points_ca;

cir_can2 = cir_can2 + points_ca2;

cir_can3 = cir_can3 + points_ca3;

cir_can4 = cir_can4 + points_ca4;

 

 

 

 

pfound=pcounter;

cir_all = cir_all + 1;

//sdistance = 1000;

}

//if (strt_po==217001) cout << int(cir_can) << " " << int(cir_can2) << " " << int(cir_all) << ";";

//-----------------------------------------------

 

 

 

//sdistance = sdistance / (endcount-pcounter-1)*8;

sdistance=0;

 

 

 

 

////////****************first_c =2

 

//**********locating low similarity

//if ( (((y_gra_max - y_gra_min) / 2 + y_gra_min - sdistance)<y_cur_vol) && (crosing3 == 1)){   //locating low similarity

//if (( (y_gra_max - y_gra_min) / 2 + y_gra_min - sdistance)>=y_cur_v2) {

//wvrepxl[pcounter]=maxminx[pcounter]-maxminx[first_c];

//wvrepyl[pcounter]=sdistance;

//}

//}

//erasing almost equal similarities (up to 5 pixels)

//********************************************************

//for (pnum=0;pnum<(pcounter);pnum++) {

//if (wvrepxl[pnum] != 0) {

//fraction = modf((wvrepxl[pcounter]%wvrepxl[pnum]), &integer);

 

//if (fraction*wvrepxl[pnum]<=10) {    //if a multipied wave length

//wvrepxl[pcounter] = 0;

//wvrepyl[pcounter] = 0;

//}

//}

 

//}

cir_canm=0;

if (cir_can>cir_canm) {cir_canm=cir_can; cir_canno=1; cir_candi=cir_dis;}

if (cir_can2>cir_canm) {cir_canm=cir_can2; cir_canno=2; cir_candi=cir_dis2;}

if (cir_can3>cir_canm) {cir_canm=cir_can3; cir_canno=3; cir_candi=cir_dis3;}

if (cir_can4>cir_canm) {cir_canm=cir_can4; cir_canno=4; cir_candi=cir_dis4;}

 

 

//*******locating high similarity

//TRYTRYif ((( (y_gra_max - y_gra_min) / 2 + y_gra_min - sdistance)>= y_cur_vol) && (crosing3 == 1)) {

crosing=0;

if (cir_canm*100 / cir_all > 65){

//finding if crossing of middle line was done after third circle

if((cir_canno==1) || (cir_canno==4)) {

for (pcount2=first_c;pcount2<=pcounter;pcount2++) {

 

if ((crosing==0) && (maxminy[first_c] >= y_var2) && (maxminy[pcount2]<=y_var2)) crosing=1;

if ((crosing==0) && (maxminy[first_c] <= y_var2) && (maxminy[pcount2]>=y_var2)) crosing=1;

if ((crosing==1) && (maxminy[first_c] >= y_var2) && (maxminy[pcount2]>=y_var2)) crosing=2;

if ((crosing==1) && (maxminy[first_c] <= y_var2) && (maxminy[pcount2]<=y_var2)) crosing=2;

//if ((strt_po == 34001) && (crosing==2)) cout << pcount2 << " " << first_c << '\n';

}

}

 

if((cir_canno==2) || (cir_canno==3)) {

for (pcount2=first_c+1;pcount2<=pcounter;pcount2++) {

if ((crosing==0) && (maxminy[first_c] >= y_var2) && (maxminy[pcount2]<=y_var2)) crosing=1;

if ((crosing==0) && (maxminy[first_c] <= y_var2) && (maxminy[pcount2]>=y_var2)) crosing=1;

if ((crosing==1) && (maxminy[first_c] >= y_var2) && (maxminy[pcount2]>=y_var2)) crosing=2;

if ((crosing==1) && (maxminy[first_c] <= y_var2) && (maxminy[pcount2]<=y_var2)) crosing=2;

}

}

//cout << "B";

 

if (crosing == 2) {

if (cir_canno==1) wvrepx[pcounter]=maxminx[pcounter]-maxminx[first_c];

if (cir_canno==2) wvrepx[pcounter]=maxminx[pcounter]-maxminx[first_c+1];

if (cir_canno==3) wvrepx[pcounter]=maxminx[pcounter+1]-maxminx[first_c+1];

if (cir_canno==4) wvrepx[pcounter]=maxminx[pcounter+1]-maxminx[first_c];

wvrepy[pcounter]=sdistance;

wvrepe[pcounter]=cir_all-cir_can;

}

 

 

 

//erasing almost equal similarities (up to 5 pixels)

//********************************************************

for (pnum=0;pnum<(pcounter);pnum++) {

if (wvrepx[pnum] != 0) {

 

//CHECKCHECK

if (((wvrepx[pcounter] / wvrepx[pnum]>=1.9) && (wvrepx[pcounter] / wvrepx[pnum]<=2.1)) || ((wvrepx[pcounter] / wvrepx[pnum]>=2.85) && (wvrepx[pcounter] / wvrepx[pnum]<=3.15))) {    //if a multipied wave length

wvrepx[pcounter] = 0;

wvrepy[pcounter] = 0;

wvrepe[pcounter] = 0;

}

 

//if (wvrepe[pnum]<wvrepe[pcounter]) {    //if a worse recognition result

//wvrepx[pcounter] = 0;

//wvrepy[pcounter] = 0;

//wvrepe[pcounter] = 0;

//}

 

 

}

}

 

}

 

 

 

//***********printing high similarity

if (cir_canm*100 / cir_all > 65){

if (wvrepx[pcounter]>0) {

 

prnt1 = float((volume / (x_get / 2)) / 1000);

prnt2 = float(strt_po / 1000);

sim_kind=2;

if (prev_stp != strt_po) result_no++;

 

if ((prev_stp==strt_po) && (cir_all-cir_canm==result[2][result_no])) result_no++;

//if (prev_strt_po!=strt_po) result_no++;

//if((cir_can*100/cir_all) > (perc2)) {

          //fprintf(stream, "%6.1f%6i%6i%6i%6i%6.1f%6.1f%6.1f%6i%6.1f\n", maxminx[pcounter+1] - maxminx[first_c] + dist_s/cir_all, formant1x-formant1xp, formant1xn-formant1x, formant2x-formant2xp, formant2xn-formant2x,prnt1, prnt2, sim_kind, int(cir_can*100/cir_all), float(scal_y));

//result[1][result_no]= float(maxminx[pcounter+1] - maxminx[first_c] + dist_s/cir_all);

if ((prev_stp != strt_po) || ((prev_stp==strt_po) && (cir_all-cir_canm==result[2][result_no-1])) || ((prev_stp==strt_po) && (cir_all-cir_canm<result[2][result_no]))) {

  if ((prev_stp==strt_po) && (cir_all-cir_canm<result[2][result_no])) {

  for(a=result_no;a>0;a--) {

  if (result[1][a]!=prnt2) break;

  }

  result_no=a+1;

  }

 

 

 

//if (cir_can*100 / cir_all > cir_can2*100 / cir_all){

if (cir_canno==1) result[0][result_no]= float(maxminx[pcounter] - maxminx[first_c]);

if (cir_canno==2) result[0][result_no]= float(maxminx[pcounter] - maxminx[first_c+1]);

if (cir_canno==3) result[0][result_no]= float(maxminx[pcounter+1] - maxminx[first_c+1]);

if (cir_canno==4) result[0][result_no]= float(maxminx[pcounter+1] - maxminx[first_c]);

//}

//else

//{

//result[1][result_no]= float(maxminx[pcounter] - maxminx[first_c+1]);

//}

if (result[0][result_no]<=0) result[0][result_no]=1;

result[1][result_no]= prnt2;

result[2][result_no]= cir_all-cir_canm;

result[3][result_no]= cir_all;

result[4][result_no]= result[4][result_no]+cir_candi/cir_canm;

prev_stp=strt_po;

//cout << result_no << " ";

}

}

//        }

//        else

//        {

          //fprintf(stream, "%6.1f%6i%6i%6i%6i%6.1f%6.1f%6.1f%6i%6.1f\n", maxminx[pcounter+2] - maxminx[first_c+1] + dist_s2/(cir_all-1), formant1x-formant1xp, formant1xn-formant1x, formant2x-formant2xp, formant2xn-formant2x,prnt1, prnt2, sim_kind, int(perc2),float(scal_y));

//result[1][result_no]= float(maxminx[pcounter+2] - maxminx[first_c+1] + dist_s2/(cir_all-1));

//result[1][result_no]= float(maxminx[pcounter+2] - maxminx[first_c+1]);

//result[2][result_no]= sim_kind;

//result[3][result_no]= float(cir_can*100/cir_all);

//result[4][result_no]= float(scal_y);

//result[5][result_no]= prnt2;

//result[6][result_no]= cir_can2;

//result[7][result_no]= pfound;

//result[8][result_no]= cir_all-1;

 

//        }

          //fwrite(buf, strlen(msg)+1, 1, stream);

          highsim=1;

}

///**************

//prints low similarity (if exists) if high similarity was not found

 

 

//}

 

}

 

 

  } //TRY2

  /////*********************

  /////end of second run : checking similarity between waves

 

 

 

 

 

//cout << strt_po << " ";

   if (strt_po>endrun) strt_po=(-(1));

 

 

 

}

   f1.close();

 

//getch();

result_end=result_no;

//avg_multip=0;

//avg_no=0;

 

 

//if (result[1][result_no]==0) break;

//}

//getch();

 

//printing the final results

for(result_no=1;result_no<1000;result_no++) {

if (result[0][result_no]!=(-1)) {

//cout << "D";

fprintf(stream, "%6.1f%6.1f%6.1f%6.1f%6.1f\n",

result[0][result_no],

result[1][result_no],

result[2][result_no],

result[3][result_no],

result[4][result_no]);

//cout << result[1][result_no] << " ";

}

if (result[0][result_no]==0) break;

}

 

//fwrite(buf, strlen(msg)+1, 1, stream);

 

 

//getch();

fclose(stream);

 

//return(1);

}

 

 

 

 

 

 

 

 

 

 

 

void calc_coords(void)

{

  // Set global variables for drawing

 

  //x_scr_max = getmaxx();

  x_scr_max = 639;

  //cout << x_scr_max << " lll ";

  y_scr_max = 199;                //max pixels of screen

  //y_scr_max = getmaxy();                //max pixels of screen

  //cout << y_scr_max << " lll ";

 

  x_gra_min = (x_scr_max * 1 / 10);

  y_gra_min = (y_scr_max * 1 / 10);

  x_gra_max = (x_scr_max * 9 / 10);

  y_gra_max = (y_scr_max * 9 / 10);     //min and max pixels of graph

 

}

 

 

void get_key()

{

  getch();

}

 

 

 

 

double y_to_pix()

{

y_var1 = y_gra_max - ((y_get - y_no_min) * (y_gra_max - y_gra_min) / (y_no_max - y_no_min));

y_var2 = (y_gra_max - y_gra_min) / 2 + y_gra_min;

y_pix = (y_var1 - y_var2) * scal_y + y_var2;

return(y_pix);

}

 

double x_to_pix()

{

x_pix = (x_get - x_no_min) * (x_gra_max - x_gra_min) / (x_no_max - x_no_min) * (double(scal_x) + 100) / 100 + x_gra_min;

return(x_pix);

}