为了完成我的项目,我需要一个包含一个返回向量(STL)的函数。使用http://en.cppreference.com中的联机编译器运行时,以下函数会返回向量:
#include <cstddef>
#include <utility>
#include <numeric>
#include <complex>
#include <valarray>
#include <vector>
#include <iostream>
typedef std::complex<double> Complex;
typedef std::valarray <Complex> CArray;
int long Gidx=0;
std::vector<int long>ar;
std::vector<int long> tet(CArray& y) {
auto max_index = std::max_element (std::begin(y), std::end(y),
[](const Complex& a ,const Complex& b)
{
return a.real() < b.real() ;
}
);
std::cout << "index of first max element is " << max_index-std::begin(y)+1 << '\n';
std::cout << "indices of all matches of max element is: " << "[";
for (auto it= std::begin(y), end = std::end(y); it != end; ++it){
if(it->real() == max_index->real()) {
std::cout << it - std::begin(y) +1 <<' ' ;
Gidx= it - std::begin(y) +1;
ar.push_back(Gidx);
}
}
std::cout << "]"<<std::endl;
return ar;
}
int main() {
CArray m({{0.12,1},{0.04,4},{0.12,6},{0.01, 2}, {0.03, 4}, {0.12, 0}, {0.09, 0}, {0.07, 0}, {0.09, 0},{0.12,4}});
std::vector<int long> Gidx4=tet(m);
//std::valarray<int long>Gidx5 = Gidx4;
//std::cout << Gidx4[3] << std::endl;
//double ix=Gidx4[1];
//std::cout << ix << std::endl;
}
输出:
index of first max element is 1
indices of all matches of max element is: [1 3 6 10 ]
奇怪的是,当我尝试在程序中使用相同的代码部分(见下文)时,结果是不同的(它只返回向量的最后一个元素),而向量包含5个元素
我的节目:
//iterDelayEst.cpp
#include <cstddef>
#include <utility>
#include <numeric>
#include <vector>
#include "myexternval.cpp"
/***********************get all indices corresponding to the max element*********************
*********************************************************************************************/
std::vector<int long> tet(CArray& y) {
auto max_index = std::max_element (std::begin(y), std::end(y),
[](const Complex& a ,const Complex& b)
{
return a.real() < b.real() ;
}
);
std::cout << "index of first max element is " << max_index-std::begin(y)+1 << '\n';
std::cout << "indices of all matches of max element is: " << "[";
for (auto it= std::begin(y), end = std::end(y); it != end; ++it){
if(it->real() == max_index->real()) {
std::cout << it - std::begin(y)+1 <<' ';
Gidx= it - std::begin(y)+1;
ar.push_back(Gidx);
}
}
std::cout <<"]"<<std::endl;
return ar;
}
/*******************************xcorr function*************************************************
**********************************************************************************************/
/*
void xcorr(CArray& x, int n){
int i;
fft(x);
x *=x.apply(std::conj);
ifft(x);
for ( i = 0 ; i < n ; i++ ){
cout << "x[" << i <<"] =" << x[i] << endl;
}
}
*/
/******************************iterDelayEst*****************************************************
************************************************************************************************/
double iterDelayEst(int n,CArray& x, CArray& y)
{ /***************************constants************************************************/
//exit if uncertainty below threshold
int j;
double thr_samples = 1e-7;
double halfN= floor (n/2);
//exit after fixed number of iterations
double nIter = 25;
fft(x);
fft(y);
//frequency domain representation of signals
std::vector<double> tau;
auto f = binFreq(n);
std::vector<double> e;
Complex nf4(0.0,0.0);
Complex nf5(0.0,0.0);
for ( j = 0 ; j < n ; j++ )
{
auto nfa=(x * x.apply(std::conj));
nf4 +=nfa[j];
auto nfb=(y * y.apply(std::conj));
nf5 +=nfb[j];
}
auto nf1 = (nf4 * nf5);
auto nf2 =std::sqrt(nf1);
auto nf =nf2/(double)n;
cout << "nf1" << nf1 <<endl;
cout << "nf2" << nf2 <<endl;
cout << "nf" << nf <<endl;
double x1=-1;
double x2=-1;
double x3=-1;
double y1=-1;
double y2=-1;
double y3=-1;
int i;
/****************************iteration loop*************************************
*******************************************************************************/
//for(i=0; i<nIter; i++)
// std::vector<complex<double> > v;
/****crosscorrelation with time-shifted signal****
*************************************************/
x = x.apply(std::conj);
y *= x;
ifft(y);
y =std::abs(y);
y=y/nf;
for ( i = 0 ; i < n ; i++ ){
cout << "y[" << i <<"] =" << y[i] << endl;
}
std::vector<int long> Gidx4=tet(y);
double ixLow =fmod((ix -1)-1 , n) +1; //one below
double ixMid =ix;
double ixHigh =fmod((ix -1)+1 , n) +1; //one above
//delay corresponding to the three bins
double tauLow =fmod(ixLow -1 + halfN, n) - halfN;
double tauMid =fmod(ixMid -1 + halfN, n) - halfN;
double tauHigh =fmod(ixHigh -1 + halfN, n) - halfN;
}
输出:
...
(-1.78919e-15,-1.78435e-15)
(-1.34929e-16,1.98328e-16)
(-1.46056e-15,1.58589e-15)
nf1(147456,0)
nf2(384,0)
nf(12,0)
y[0] =(0.583333,0)
y[1] =(0.583333,0)
y[2] =(0.5,0)
y[3] =(0.416667,0)
y[4] =(0.333333,0)
y[5] =(0.333333,0)
y[6] =(0.333333,0)
y[7] =(0.333333,0)
y[8] =(0.416667,0)
y[9] =(0.416667,0)
y[10] =(0.5,0)
y[11] =(0.583333,0)
y[12] =(0.416667,0)
y[13] =(0.333333,0)
y[14] =(0.25,0)
y[15] =(0.0833333,0)
y[16] =(0.0833333,0)
y[17] =(0.166667,0)
y[18] =(0.333333,0)
y[19] =(0.5,0)
y[20] =(0.5,0)
y[21] =(0.5,0)
y[22] =(0.583333,0)
y[23] =(0.583333,0)
y[24] =(0.5,0)
y[25] =(0.416667,0)
y[26] =(0.25,0)
y[27] =(0.0833333,0)
y[28] =(0.0833333,0)
y[29] =(0.166667,0)
y[30] =(0.333333,0)
y[31] =(0.5,0)
index of first max element is 23
indices of all matches of max element is: [23 ]
serge@ubuntu:~/Downloads/OpenCV/opencv-2.4.9/build$
有人可以帮助我吗?
我做错了什么?
答案 0 :(得分:0)
为什么你说这与返回矢量有关?在我阅读您的代码时,std::max_element
在索引23处找到最大值,这是最后出现的0.583333。然后,您将使用==
将其他出现的0.583333与最后一次出现进行比较。你确定所有出现的0.583333都完全相等吗?通常最好将浮点数与某些epsilon进行比较,而不是要求完全相等。
为了测试我的理论,在for循环中你可以打印出it->real()
和==
比较的布尔结果,看你是否得到一个完全匹配或五个完全匹配。 / p>