C++ - Complex Value mistake, computing Cross Spectral Density (CSD) - c++

Dear community,
I am facing a rather annoying problem. I am calculating the Cross Spectral Density (CSD) between two time signals, which were already proccessed with FFT to two complex frequency vectors(Singal1 =>freqvec, Signal2 => freqvec2).
RowVectorXcd CSD(n_Epochs, fftsize);
for(int j = 0; j < fftsize; j++) {
std::complex<double> cospectrum = freqvec(j).real() * freqvec2(j).real() + freqvec(j).imag() * freqvec2(j).imag() ;
std::complex<double> quadspectrum = freqvec(j).real() * freqvec2(j).imag() - freqvec(j).imag() * freqvec2(j).real() ;
std::cout << "cospectrum:"<<cospectrum<< std::endl;
CSD(j) = sqrt( pow( cospectrum, 2 ) + pow( quadspectrum, 2) ) ;
For further computations I need to get the imaginary part of this calculation correctly.
The calculation does work, but somehow the result always has an imaginary value of zero.

Related

dft (discrete fourier transform) with C++ code

I programmed c in Visual studio c++.
In visual studio c++, I operate complex number with <complex> at c code grammar, not c++.
a[] is 4001 array, so using b[4001] store operated value and finally return a[].
NXm is defined 4001 from main.
when I compared with the result of matlab's fft, The difference occurs from the 169th to 4000th value.
Would you see if there is any error? or what is the cause?
Thank you for reading the question.
I've tried reversely progressing "for", from NXm to 0.
I've tried changing double ak = (double)k * (double)n * (2.0 * M_PI / (double)NXm); to double ak = k * n * (2.0 * M_PI / (double)NXm);
When I've tried operating short length of field, function worked well.
This is the code.
void fft(complex<double> a[], int NXm)
{
complex<double> sum = 0.0 + 0.0*I; ;
complex<double> c = 1.0*I;
complex<double> b[4001] = { 0 };
for (int k = 0; k < NXm; k++)
{
sum = 0.0 + 0.0*I;
for (int n = 0; n < NXm; n++)
{
double ak = (double)k * (double)n * (2.0 * M_PI / (double)NXm);
sum = sum + a[n] * exp(-c * ak);
}
b[k] = sum;
}
for (int i = 0; i < NXm; i++)
{
a[i] = b[i];
}
}
I expect almost same result as fft of matlab.
Slight error of epsilon level is ok
Complex exponential is:
exp(x + I*y) = exp(x) ( cos(y) + I*sin(y)) = exp(x) * cis(y)
problem may be caused by a flaw in FPU part of processor, if y is rather small, built-in a intrinsic function for sin(y) got precision worse than |y - sin(y)|. Naive implementations of Euler formula may err with small arguments, sloppy ones may assume that sin(y) is zero.
It's better to replace sin() by cosecant intrinsic, which is a reciprocal sinus (1/sin(x)).
Another, less common issue is when trig functions are supplied by values which are greater than 2*PI*n, where n is a natural greater than 1. Precision drops with increase of n and sin (x) != sin(2*PI*n + x)
Third, depending on compiler, there might be optimizing flags which regulate how compiler treats use of math functions and floating point math in general. They may default to imprecise but fast calculations. This may supercede with use of C header in C++ code.
MATLAB used FFT (Fast Fourier Transform) algorithm for computing DFT. It is both much faster (O(n log n) as opposed to O(n^2) with the direct method) and more stable numerically. So don't expect the same results as MATLAB makes.

C++ - Complex Value Calculation Error in Cross Spectral Density

I am a very beginner in c++ and I want to do some spectral calculations, in this case calculating the 'Cross Spectral Density' of two signal (vecFirst, vecSecond), which are already processed with a FastFourierTransformation. Resulting in freqvec and freqvec2, containing complex values for each frequency.
For this calculating it is essential to keep every value as a complex value. E.g.: CoSpectrum, which is calculated in line 6, should has a complex value as a result.
RowVectorXcd freqvec;
RowVectorXcd freqvec2;
fft.fwd(freqvec, vecFirst);
fft.fwd(freqvec2, vecSecond);
// # Create conjugate complex
freqvec.conjugate();
freqvec2.conjugate();
RowVectorXcd Rxy(freqvec.cols());
for (int i = 0; i < freqvec.cols(); i++) {
std::complex<double>CoSpectrum( freqvec(i).real() * freqvec2(i).real() + freqvec(i).imag() * freqvec2(i).imag()) ;
std::complex<double>QuadSpectrum( freqvec(i).real() * freqvec2(i).imag() - freqvec(i).real() * freqvec2(i).imag() ) ;
std::complex<double>CoSpectrum_sqr = CoSpectrum * CoSpectrum ;
std::complex<double>QuadSpectrum_sqr = QuadSpectrum * QuadSpectrum ;
Rxy(i) = sqrt(std::complex<double>(CoSpectrum_sqr + QuadSpectrum_sqr)) ;
}
}
Unfortunately I only get complex values with zero in the imaginary part.
Can anyone tell me why?
I am guessing the expression freqvec(i).real() only returns a double value, but how can I get the real part but keep it a complexvalue. Or, accordingly, just multiply the imaginary part of a complex number with the real part of another and keep it the result a complex double.
Thanks for any help in advance.
At first: Thank you very much. I had thought of something like that, but wasn't sure. Thank got there a nice people like you who care about beginners.
So I changed the snippet as following:
// ### Attempting to compute the Frequency Power for Frequency Bins..
RowVectorXcd freqvec;
RowVectorXcd freqvec2;
fft.fwd(freqvec, vecFirst);
fft.fwd(freqvec2, vecSecond);
std::cout<<"freqvec:"<<freqvec.cols()<<std::endl;
// ### Attempting to compute the PowerSpectralDensitiy(PSD) and CrossSpectralDensity(CSD). The cross-spectral density is the Fourier transform of the cross-correlation function.
RowVectorXcd Rxy(n_Epochs, freqvec.cols());
RowVectorXcd Rxx(n_Epochs, freqvec.cols());
RowVectorXcd Ryy(n_Epochs, freqvec.cols());
for (int i = 0; i < n_Epochs; i++) {
std::complex<double>CoSpectrum( std::complex<double>(freqvec(i).real(),0) * std::complex<double>(freqvec2(i).real(),0) + std::complex<double>(0,freqvec(i).imag()) * std::complex<double>(0, freqvec2(i).imag()) ) ;
std::complex<double>QuadSpectrum( std::complex<double>(freqvec(i).real(), 0) * std::complex<double>(0, freqvec2(i).imag()) - std::complex<double>(0,freqvec(i).imag()) * std::complex<double>(freqvec2(i).real(), 0) ) ;
std::complex<double>CoSpectrum_sqr = CoSpectrum * CoSpectrum ;
std::complex<double>QuadSpectrum_sqr = QuadSpectrum * QuadSpectrum ;
Rxy(i) = sqrt(std::complex<double>(CoSpectrum_sqr + QuadSpectrum_sqr)) ;
Rxx(i) = std::complex<double>(freqvec(i).real(), 0) * std::complex<double>(freqvec(i).real(), 0) + std::complex<double>(0, freqvec(i).imag()) * std::complex<double>(0, freqvec(i).imag()) ;
Ryy(i) = std::complex<double>(freqvec2(i).real(), 0) * std::complex<double>(freqvec2(i).real(), 0) + std::complex<double>(0, freqvec2(i).imag()) * std::complex<double>(0, freqvec2(i).imag()) ;
}
}
This solved my problem. Thanks again for the nice discussion.

bandpass FIR filter

I need to make a simple bandpass audio filter.
Now I've used this simple C++ class: http://www.cardinalpeak.com/blog/a-c-class-to-implement-low-pass-high-pass-and-band-pass-filters
It works well and cut off the desired bands. But when I try to change upper or lower limit with small steps, on some values of limit I hear the wrong result - attenuated or shifted in frequency (not corresponding to current limits) sound.
Function for calculating impulse response:
void Filter::designBPF()
{
int n;
float mm;
for(n = 0; n < m_num_taps; n++){
mm = n - (m_num_taps - 1.0) / 2.0;
if( mm == 0.0 ) m_taps[n] = (m_phi - m_lambda) / M_PI;
else m_taps[n] = ( sin( mm * m_phi ) -
sin( mm * m_lambda ) ) / (mm * M_PI);
}
return;
}
where
m_lambda = M_PI * Fl / (Fs/2);
m_phi = M_PI * Fu / (Fs/2);
Fs - sample rate (44.100)
Fl - lower limit
Fu - upper limit
And simple filtering function:
float Filter::do_sample(float data_sample)
{
int i;
float result;
if( m_error_flag != 0 ) return(0);
for(i = m_num_taps - 1; i >= 1; i--){
m_sr[i] = m_sr[i-1];
}
m_sr[0] = data_sample;
result = 0;
for(i = 0; i < m_num_taps; i++) result += m_sr[i] * m_taps[i];
return result;
}
Do I need to use any window function (Blackman, etc.)? If yes, how do I do this?
I have tried to multiply my impulse response to Blackman window:
m_taps[n] *= 0.42 - 0.5 * cos(2.0 * M_PI * n / double(N - 1)) +
0.08 * cos(4.0 * M_PI * n / double(N - 1));
but the result was wrong.
And do I need to normalize taps?
I found a good free implementation of FIR filter:
http://www.iowahills.com/A7ExampleCodePage.html
...This Windowed FIR Filter C Code has two parts, the first is the
calculation of the impulse response for a rectangular window (low
pass, high pass, band pass, or notch). Then a window (Kaiser, Hanning,
etc) is applied to the impulse response. There are several windows to
choose from...
y[i] = waveform[i] × (0.42659071 – 0.49656062cos(w) + 0.07684867cos(2w))
where w = (2)i/n and n is the number of elements in the waveform
Try this I got the code from:
http://zone.ni.com/reference/en-XX/help/370592P-01/digitizers/blackman_window/
I hope this helps.

How to compute sum of evenly spaced binomial coefficients

How to find sum of evenly spaced Binomial coefficients modulo M?
ie. (nCa + nCa+r + nCa+2r + nCa+3r + ... + nCa+kr) % M = ?
given: 0 <= a < r, a + kr <= n < a + (k+1)r, n < 105, r < 100
My first attempt was:
int res = 0;
int mod=1000000009;
for (int k = 0; a + r*k <= n; k++) {
res = (res + mod_nCr(n, a+r*k, mod)) % mod;
}
but this is not efficient. So after reading here
and this paper I found out the above sum is equivalent to:
summation[ω-ja * (1 + ωj)n / r], for 0 <= j < r; and ω = ei2π/r is a primitive rth root of unity.
What can be the code to find this sum in Order(r)?
Edit:
n can go upto 105 and r can go upto 100.
Original problem source: https://www.codechef.com/APRIL14/problems/ANUCBC
Editorial for the problem from the contest: https://discuss.codechef.com/t/anucbc-editorial/5113
After revisiting this post 6 years later, I'm unable to recall how I transformed the original problem statement into mine version, nonetheless, I shared the link to the original solution incase anyone wants to have a look at the correct solution approach.
Binomial coefficients are coefficients of the polynomial (1+x)^n. The sum of the coefficients of x^a, x^(a+r), etc. is the coefficient of x^a in (1+x)^n in the ring of polynomials mod x^r-1. Polynomials mod x^r-1 can be specified by an array of coefficients of length r. You can compute (1+x)^n mod (x^r-1, M) by repeated squaring, reducing mod x^r-1 and mod M at each step. This takes about log_2(n)r^2 steps and O(r) space with naive multiplication. It is faster if you use the Fast Fourier Transform to multiply or exponentiate the polynomials.
For example, suppose n=20 and r=5.
(1+x) = {1,1,0,0,0}
(1+x)^2 = {1,2,1,0,0}
(1+x)^4 = {1,4,6,4,1}
(1+x)^8 = {1,8,28,56,70,56,28,8,1}
{1+56,8+28,28+8,56+1,70}
{57,36,36,57,70}
(1+x)^16 = {3249,4104,5400,9090,13380,9144,8289,7980,4900}
{3249+9144,4104+8289,5400+7980,9090+4900,13380}
{12393,12393,13380,13990,13380}
(1+x)^20 = (1+x)^16 (1+x)^4
= {12393,12393,13380,13990,13380}*{1,4,6,4,1}
{12393,61965,137310,191440,211585,203373,149620,67510,13380}
{215766,211585,204820,204820,211585}
This tells you the sums for the 5 possible values of a. For example, for a=1, 211585 = 20c1+20c6+20c11+20c16 = 20+38760+167960+4845.
Something like that, but you have to check a, n and r because I just put anything without regarding about the condition:
#include <complex>
#include <cmath>
#include <iostream>
using namespace std;
int main( void )
{
const int r = 10;
const int a = 2;
const int n = 4;
complex<double> i(0.,1.), res(0., 0.), w;
for( int j(0); j<r; ++j )
{
w = exp( i * 2. * M_PI / (double)r );
res += pow( w, -j * a ) * pow( 1. + pow( w, j ), n ) / (double)r;
}
return 0;
}
the mod operation is expensive, try avoiding it as much as possible
uint64_t res = 0;
int mod=1000000009;
for (int k = 0; a + r*k <= n; k++) {
res += mod_nCr(n, a+r*k, mod);
if(res > mod)
res %= mod;
}
I did not test this code
I don't know if you reached something or not in this question, but the key to implementing this formula is to actually figure out that w^i are independent and therefore can form a ring. In simpler terms you should think of implement
(1+x)^n%(x^r-1) or finding out (1+x)^n in the ring Z[x]/(x^r-1)
If confused I will give you an easy implementation right now.
make a vector of size r . O(r) space + O(r) time
initialization this vector with zeros every where O(r) space +O(r) time
make the first two elements of that vector 1 O(1)
calculate (x+1)^n using the fast exponentiation method. each multiplication takes O(r^2) and there are log n multiplications therefore O(r^2 log(n) )
return first element of the vector.O(1)
Complexity
O(r^2 log(n) ) time and O(r) space.
this r^2 can be reduced to r log(r) using fourier transform.
How is the multiplication done, this is regular polynomial multiplication with mod in the power
vector p1(r,0);
vector p2(r,0);
p1[0]=p1[1]=1;
p2[0]=p2[1]=1;
now we want to do the multiplication
vector res(r,0);
for(int i=0;i<r;i++)
{
for(int j=0;j<r;j++)
{
res[(i+j)%r]+=(p1[i]*p2[j]);
}
}
return res[0];
I have implemented this part before, if you are still cofused about something let me know. I would prefer that you implement the code yourself, but if you need the code let me know.

FIR filtering using window function: implementation problem

I want to make a FIR filter using a window function. I have some sample data and size variable is a count of samples. The windowSize variable is a size of the window function.
At first I create the window function (blackman window): the variable window
Then I need to multiply it by sin(x) / x function and convolve with real data (variable data):
for (int i = 0; i < size; ++i) {
for (j = 0; j < windowSize; ++j) {
double arg = 2.0 * PI * ((double)j - (double)windowSize / 2.0) / (double)windowSize;
if (i + j - windowSize / 2 < 0)
continue;
if (arg == 0) {
filteredData[i] += data[i + j - windowSize / 2] * window[j] * 1.0 / (double)windowSize;
} else
filteredData[i] += data[i + j - windowSize / 2] * window[j] * (sin(arg) / arg) / (double)windowSize;
}
}
The problem:
As a result I get a filtered data with an average which very different than the average of the original data. Where is a mistake?
In the DSP book it is written that in order to make a FIR filter we should multiply the function sin(x) / x by a window function and then perform the convolution, but nothing is written about x in the sin(x) / x, so I used the:
double arg = 2.0 * PI * ((double)j - (double)windowSize / 2.0) / (double)windowSize;
for the x value, the argument of sine, is it correct?
The sin(x)/x filter is a lowpass filter. That is, it suppresses all frequencies above a certain cutoff frequency.
If the sampling frequency is Fs (Hertz) and you want a cutoff frequency of fc (Hertz), You should be using x = 2*PI*fc/(2*Fs)*n where n goes from -N to +N and N is large enough that the sin(x)/x function is close to zero. Don't forget that sin(x)/x is 1 when x is zero.
To maintain the average of the signal you have to normalize the filter coefficients by their sum. I.e., set f_norm[k] = f[k] / sum(f[k], k=...)
That's all I have to say at this point. It seems like you have a lot to learn. I suggest a good book on signal processing.
As far as the implementation is concerned it looks like you need to initialise filteredData[i], e.g.
for (int i = 0; i < size; ++i) {
filteredData[i] = 0;
for (j = 0; j < windowSize; ++j) {
...