if statements

i am trying to extract the values from filterData vector. I want to pull out the values and the index they are located at but i am stuck at what to actually put inside the if statement. if there is a better way i am open to trying that too. but basically im trying to find the peaks of my vector. then i want to find the distance between each peak in terms of number of indexes.

thanks

1
2
3
4
5
6
7
8
9
10
11
  M = 0.75 * max;
    
    for (i=0; i<countlines; i++)
    {
       if(filterData[i-1] < filterData[i] && filterData[i+1] < filterData[i] && filterData[i] > M)
        {
            cout << i << "    : " <<filterData[i] <<endl;
            
            
        }
    }
Last edited on
You don't know how many peaks there will be, so you can record their indices in a vector<int>; you can push_back( i ) into that collection every time that you identify a peak.

Your method as given will, however, crack up in several circumstances:
- your peak may occur at one or other end, so you won't always be able to test both neighbours;
- you will have to decide what to do with a "plateau" (several values the same).
i tried using pushback but im not sure if im implementing it wrong. would you mind showing me how i would use it here?
You haven't given enough code to show how to implement a vector and push_back().

Please also take into account the comments I made about identifying peaks. At present, your method may fail to identify quite a few, independent of storing them
the peaks will be fine since the data im working with wont have any plateau.

if its any help this is what i currently have, ignore the classes i am just trying to find a way to implement the concept of classes into the work.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

class ECG {
public:
    
    
};

class Resp {
    
    
};




int countlines= 0;

void number(){
    countlines--;
    cout << "lines: " << countlines << endl;
};

int main(){
    vector<double> data;
    
    double values, sum = 0.0, max, M;
    string line;
    int i, j, k;
   
    
    ifstream rawECGdata;
    ifstream RESPdata;
    rawECGdata.open("ecg.txt");
    RESPdata.open("resp.txt");
    
    if(!rawECGdata.is_open()){
        cout << "error cant open " << endl;
        }
    
    while(rawECGdata >> values){
        countlines++;
        data.push_back(values);
        }
    
    vector<double> filterData(countlines);
    for (i=0; i<=countlines; i++)
    {
        for (k=0; k<=50; k++)
        {
            j = (i+k-25);
            if ((j > 0)&&(j<countlines))
            {
                sum = sum + data[j];
            }
        }
        filterData[i] = (1.0/50.0) * sum;
        sum = 0.0;
    }
    
    max = *max_element(filterData.begin(),filterData.end());
    cout << "max value: " << max << endl;
    
    M = 0.75 * max;
    cout << M << endl;
    
    
    
    for (i=0; i<countlines; i++)
    {
        vector<int>index;
        if(filterData[i-1] < filterData[i] && filterData[i+1] < filterData[i] && filterData[i] > M)
        {
            index.push_back(i);
            
            cout << i << "    : " <<filterData[i] <<endl;
            
        }
    }
}





Give or take some typos ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
    vector<int> peakIndices;
    for (i=0; i<countlines; i++)
    {
       if ( filterData[i] > M )
       {
          if ( ( i == 0                      && filterData[i+1] < filterData[i] ) ||
               ( i == countlines - 1         && filterData[i-1] < filterData[i] ) ||
               ( i > 0 && i < countlines - 1 && filterData[i-1] < filterData[i] && filterData[i+1] < filterData[i] ) )
             peakIndices.push_back( i );
       }
    }

    cout << "Peak index : value\n";
    for ( int i : peakIndices ) cout << i << " :  " << filterData[i] << '\n';
okay thank you that helps a lot
Topic archived. No new replies allowed.