Convert Matlab Hilbert to C ++

First of all, please excuse my ignorance in this area, I am a programmer by profession, but I am stuck in a situation slightly higher than my experience (in mathematics and signal processing).

I have a Matlab script that I need to port to a C ++ program (without compiling matlab code into a DLL). It uses the hilbert() function with one argument. I am trying to find a way to implement the same thing in C ++ (i.e. have a function that also takes only one argument and returns the same values).

I read about how to use FFT and IFFT to create it, but it may not seem like it's as simple as the Matlab version. The main thing is that I need him to work on a 128 * 2000 matrix, and nothing that I found in my search showed me how to do this.

I would be fine with returning a complex value or just an absolute value. The easier it is to integrate into the code, the better.

Thanks.

+4
source share
4 answers

The MatLab hilbert () function does not actually calculate the Hilbert transform directly, but instead calculates the analytical signal, which is needed in most cases. He does this by taking the FFT, removing the negative frequencies (by setting the top half of the array to zero) and applying the inverse FFT. It would be right in C / C ++ (three lines of code) if you have a decent implementation of FFT.

+8
source

Simple code below. (Note: this was part of a large project). The value of L is based on your definition of your order, N. With N = 2L-1. Round N is an odd number. The xbar below is based on the signal that you define as the input signal for your developed system. This was implemented in MATLAB.

 L = 40; n = -L:L; % index n from [-40,-39,....,-1,0,1,...,39,40]; h = (1 - (-1).^n)./(pi*n); %impulse response of Hilbert Transform h(41) = 0; %Corresponds to the 0/0 term (for 41st term, 0, in n vector above) xhat = conv(h,xbar); %resultant from Hilbert Transform H(w); plot(abs(xhat)) 
0
source

Not the right answer to your question, but perhaps a better way to sleep. I believe that you cannot be much faster than Matlab in the specific case of what is basically ffts on the matrix. That's where Matlab excels!

Matlab FFTs are computed using FFTW, the de facto fast FFT algorithm written in C, which is apparently also parallelized by Matlab. Also, citing http://www.mathworks.com/help/matlab/ref/fftw.html :

For FFT sizes that have a value of 2, between 214 and 222, MATLAB software uses special preloaded information in an internal database to optimize FFT calculations.

So don’t feel bad if your code is a little slower ...

0
source

All Articles