5
\$\begingroup\$

I use this piece of code to compute a short-time Fourier transform:

// Output pre-allocation
std::vector<std::vector<std::complex<T> > > y(nFft, std::vector<std::complex<T> >(nFrames, 0.0));
std::vector<T> xt;
std::vector<std::complex<T> > yt;
xt.reserve(nFft);
yt.reserve(nFft);
#pragma omp parallel for private(xt,yt)
for (unsigned int t = 0; t < nFrames; ++t) {
 const int offset = -((int) wSize/2) + t*nHop;
 // Extract frame of input data
 if (offset < 0) {
 xt.assign(x.begin(), x.begin() + offset + wSize);
 xt.resize(wSize, 0.0);
 }
 else if (offset + wSize > n) {
 xt.assign(x.begin() + offset, x.end());
 xt.resize(wSize, 0.0);
 }
 else
 xt.assign(x.begin() + offset, x.begin() + offset + wSize);
 // Apply window to current frame
 std::transform(xt.begin(), xt.end(), w.begin(), xt.begin(), std::multiplies<T>());
 // Zero padding
 std::rotate(xt.begin(), xt.begin() + wSize/2, xt.end());
 xt.insert(xt.begin() + wSize/2, nFft - wSize, 0.0);
 yt = fft(xt); // Perform the FFT!
 #pragma omp critical
 {
 for (unsigned int f = 0; f < nFft; ++f)
 y[f][t] = yt[f];
 }
}

Is there something that I can improve, either in design style or performances, without sacrificing readability? Of course the point is to improve my coding skills without using any external library!

Regarding the xt and yt vectors, I put them outside the loop to improve the performances in mono-threaded (when OpenMP is disabled). In multi-threaded, there are copied for each thread anyway (hence the use of the private close).

200_success
145k22 gold badges190 silver badges478 bronze badges
asked Dec 19, 2011 at 10:54
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

The only thing worth commenting on is the identifier names could be more meaningful.

  • Short identifiers are harder to find when maintaining the code (more false positives).
  • Short identifiers make it hard to put meaning to the identifier.
answered Dec 19, 2011 at 16:34
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.