Hilbert Transformer processing block by block I am processing audio and trying to obtain a 90º phase shift when processing block by block.

I have implemented a Hilbert Transformer (from Lyon's 3rd. Ed. &amp; MATLAB). MATLAB code below (I believe this is similar to what MATLAB hilbert does).

function result = my_hilbert(s)
    fprintf("Using My Hilbert\n");
    n = length(s)

    %fftLength = 2 ^ nextpow2(2 * n - 1);
    fftLength = n

    H = zeros(1, fftLength);
    H(1) = 1;
    H(2:n / 2) = 2;
    H(n / 2 + 1) = 1;

    x = zeros(1, fftLength);
    x(1:n) = s
    X = fft(x)

    Y = X .* H
    y = ifft(Y)

    result = y(1:n);
end


This implementation seems to assume one is processing the complete audio stream in a single call.

I need to be able to achieve a phase shift but will be processing one block (block size between 1024 and 2048 samples) at a time.

When I simply call this implementation on each block I receive audible pops around the block boundaries.

I have considered overlap+add but when using an FFT length other than length of the input, the results are no longer the same as MATLAB's hilbert() function. 

How can one obtain a 90º phase shift when processing block by block without these discontinuities?
The key to block (or stream) filtering in Matlab is using the filter's command ability to return and reuse its internal state.

To illustrate, first create a filter that implements the Hilbert transform.

hilb = designfilt('hilbertfir','FilterOrder',60, ...
       'TransitionWidth',400,'SampleRate',1e4);


This example is taken directly from the documentation (type doc hilbert in Matlab).

Now, we need this form of the filter command (see doc filter):

[y, zo] = filter(b,a,x,zi)


which uses initial conditions zi and returns final conditions in zf.

To put everything together, say you have incoming data and store it in variable block. Then, you can do something like:

block = acquire_block_of_data();
[output, zo] = filter(hilb, 1, block);
while 1
    block = acquire_block_of_data();
    zi = zo;
    [output, zo] = filter(hilb, 1, block, zi)
end


Note that we need an initial call to filter without state; alternatively, you can set let zi have an initial value equal to a vector of zeros with the same length as the filter order.