【发布时间】:2017-01-13 05:31:14
【问题描述】:
为了完成我的项目,我需要一个返回向量 (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$
有人可以帮我吗?
我做错了什么?
【问题讨论】:
-
调试器是解决此类问题的正确工具。 在询问 Stack Overflow 之前,您应该逐行逐行检查您的代码。如需更多帮助,请阅读How to debug small programs (by Eric Lippert)。至少,您应该 [编辑] 您的问题,以包含一个重现您的问题的 Minimal, Complete, and Verifiable 示例,以及您在调试器中所做的观察。
-
使用
at方法而不是[]来查找超出范围的数组。这是一个开始。
标签: c++ function vector stl complex-numbers