FFT

Apr 3, 2013 at 9:22pm
Quick question:

I'm using this code inside of a class:
1
2
3
4
5
6
7
8
9
10
11
12
std::vector<int> signalIn;

void AddSample(int in)
{
    signalIn.push( in );

    if (signalIn.size() > m_maxSize)
        signalIn.pop(); 

    std::valarray<int> data( signalIn.data(), signalIn.size() ); 
    // ...
}


I have two questions:
1. What is the best STL container for this?
std::vector can do a .push_back() and .erase( .first() ) and supports .data() in C++11. std::queue is more effecient at push/pop, but doesn't have .data() support. My target build enviornment doesn't support .data() anyways.

2. Is there an alternative to .data? At work we are using VS2008 which has no support for C++11's .data() member of std::vector. I need to find an equivalent solution that will work in C++03.
Apr 3, 2013 at 9:49pm
1
2
3
4
int *temp = new int[signalIn.size()]; //C++03
std::copy(signalIn.begin(), signalIn.end(), temp); //C++03
std::valarray<int> data (temp, signalIn.size()); //C++03
delete[] temp, temp = 0; //C++03 
Also, I have seen several credible sources use &signalIn.front() instead of .data() (e.g. http://www.mr-edd.co.uk/blog/beginners_guide_streambuf)
Last edited on Apr 4, 2013 at 1:39pm
Apr 4, 2013 at 6:23am
I'll give .front() a try (I don't know why I didn't think of that). Thanks for the suggestion.

It's for a real-time FFT which needs to run in parallel with a bunch of other stuff so I'm trying to keep my stuff on the stack to minimize the footprint.
Apr 4, 2013 at 6:30am
Nevermind, .front() doesn't give me what I expected. I actually get different results between using .front and .data. This seems to indicate that on this compiler (g++), the memory is not stored successively or that it is padded.

The temporary array with dynamic memory allocation works. Just curious, why did you set temp = 0; instead of delete[] temp;? It looks intentional, but that would cause a memory leak no?
Apr 4, 2013 at 1:39pm
I forgot the delete statement (I got confused by the fact that valarray had taken the pointer and used it). I've edited my post now - thanks for catching that ;)
Last edited on Apr 4, 2013 at 1:40pm
Topic archived. No new replies allowed.