]> git.sesse.net Git - kdenlive/blobdiff - src/lib/audio/fftCorrelation.cpp
fix coverity 1134134 1134135 (div by 0)
[kdenlive] / src / lib / audio / fftCorrelation.cpp
index 5a28cda6f767756e75e7e710638642fc7bc488f8..139ea203d0daaac1c3a62b3949e5628222cf223b 100644 (file)
@@ -1,4 +1,4 @@
-/*
+ /*
 Copyright (C) 2012  Simon A. Eugster (Granjow)  <simon.eu@gmail.com>
 This file is part of kdenlive. See www.kdenlive.org.
 
@@ -29,7 +29,7 @@ void FFTCorrelation::correlate(const int64_t *left, const int leftSize,
     // The correlation vector will have entries up to N (number of entries
     // of the vector), so converting to integers will not lose that much
     // of precision.
-    for (int i = 0; i < leftSize+rightSize+1; i++) {
+    for (int i = 0; i < leftSize+rightSize+1; ++i) {
         out_correlated[i] = correlatedFloat[i];
     }
 }
@@ -48,14 +48,14 @@ void FFTCorrelation::correlate(const int64_t *left, const int leftSize,
     // Dividing by the max value is maybe not the best solution, but the
     // maximum value after correlation should not be larger than the longest
     // vector since each value should be at most 1
-    int64_t maxLeft = 0;
-    int64_t maxRight = 0;
-    for (int i = 0; i < leftSize; i++) {
+    int64_t maxLeft = 1;
+    int64_t maxRight = 1;
+    for (int i = 0; i < leftSize; ++i) {
         if (labs(left[i]) > maxLeft) {
             maxLeft = labs(left[i]);
         }
     }
-    for (int i = 0; i < rightSize; i++) {
+    for (int i = 0; i < rightSize; ++i) {
         if (labs(right[i]) > maxRight) {
             maxRight = labs(right[i]);
         }
@@ -64,20 +64,20 @@ void FFTCorrelation::correlate(const int64_t *left, const int leftSize,
 
     // One side needs to be reverted, since multiplication in frequency domain (fourier space)
     // calculates the convolution: \sum l[x]r[N-x] and not the correlation: \sum l[x]r[x]
-    for (int i = 0; i < leftSize; i++) {
+    for (int i = 0; i < leftSize; ++i) {
         leftF[i] = double(left[i])/maxLeft;
     }
-    for (int i = 0; i < rightSize; i++) {
+    for (int i = 0; i < rightSize; ++i) {
         rightF[rightSize-1 - i] = double(right[i])/maxRight;
     }
 
     // Now we can convolve to get the correlation
-    convolute(leftF, leftSize, rightF, rightSize, out_correlated);
+    convolve(leftF, leftSize, rightF, rightSize, out_correlated);
 
     std::cout << "Correlation (FFT based) computed in " << t.elapsed() << " ms." << std::endl;
 }
 
-void FFTCorrelation::convolute(const float *left, const int leftSize,
+void FFTCorrelation::convolve(const float *left, const int leftSize,
                                const float *right, const int rightSize,
                                float *out_convolved)
 {
@@ -123,7 +123,7 @@ void FFTCorrelation::convolute(const float *left, const int leftSize,
     kiss_fftr(fftConfig, rightData, rightFFT);
 
     // Convolution in spacial domain is a multiplication in fourier domain. O(n).
-    for (int i = 0; i < size/2; i++) {
+    for (int i = 0; i < size/2; ++i) {
         correlatedFFT[i].r = leftFFT[i].r*rightFFT[i].r - leftFFT[i].i*rightFFT[i].i;
         correlatedFFT[i].i = leftFFT[i].r*rightFFT[i].i + leftFFT[i].i*rightFFT[i].r;
     }