Forward and Backward Filtering (MATLAB's filtfilt())

I have an input sequence $x(n)$ and impulse sequence

$$h(n) = \begin{cases} 0 & \quad \text{when } n = 0 \\ .239052 & \quad \text{when } n = 1 \\1.3357\exp(-0.650286n) & \quad \text{when } n > 1\\ \end{cases}$$

I did a convolution and obtained $y(n)=x(n)*h(n)$. I trimmed some of the data in the end from $y(n)$ and then I flipped $h(n)$ and I did reverse filtering $w(n)=y(n)*h(-n)$. When I researched about this I found filtfilt in matlab is an easier option but I am trying to implement this in C. So my question is without trimming the first output sequence $y(n)$ I am getting a drifted version of the result (there is a delay).

Is it trimming really necessary or am I doing something wrong here.
One more question is I can see some transients both at the start and end of $w(n)$ sequence when I use filtfilt I don't see that transients.The reason is matlab is using some initial states based on the below paper to get rid of the transients.

Gustafsson, F. "Determining the initial states in forward-backward filtering." IEEE® Transactions on Signal Processing. Vol. 44, April 1996, pp. 988–992.

To summarize :

1. Is it trimming really necessary ? PS the figure attached without trimming the the resultant data has a lag..Trimming the $y(n)$ sequence before backward filtering aligns the result.
2. how to determine the initial states for getting rid of transients.
• SE.DSP wishes you a happy new year 2017, with a kind reminding signal that your question or its answers may require some action from you (edit, update, votes, acceptance, etc.) – Laurent Duval Jan 15 '17 at 16:24

The method in the paper is based on a simple idea, if we filter the signal $x [n]$ forward and then backward, the result should be equal to the result of doing it backward and then forward.
The paper shows how to chose the initial conditions which minimize the ${\ell}_{2}$ norm between the the different approaches.
Since your two-pass filter will be symmetric and monomodal (similar in shape to a two-sided exponential), you can estimate your "computational delay" (including all your windowing, padding, trimming, faulty allocations, subsampling...) by giving an input in the form of a Dirac-delta function, with its maximum 1 at a given location $k$, for instance far away from the edges. It could be like $$0\,0\,0\,0\,1\,0\,0\,0\,0$$ Then filter it your double-way, and find the index $l$ of the maximum in the output filtered signal. It could be longer that the original one: $$\epsilon_1\,\epsilon_2\,\epsilon_3\,\epsilon_4\,\epsilon_5\,\epsilon_6\,\epsilon_7\,\textrm{max}\,\epsilon_7\,\epsilon_6\,\epsilon_5\,\epsilon_4\,\epsilon_3\,\epsilon_2\,\epsilon_1\,$$ That $l-k$ (here $(8-5))$ gives you an integer quantity, a system delay, since your are using an LTI (linear, time-invariant) filter. Then, you can investigate where this delay comes from in your filtering process, and decide how to trim or (circularity) shift your "real signal".
For the second part, since your filter is partly exponential I would first suggest you to look at recursive implementations of exponential filters. You can split your filter $h$ as $h = h_1+h_2$ with $$h_1 = \{0\,0.239052\,0\,0\,0\, \dots\}$$ and $h_2$ the exponential part. The latter can be implemented with simple recursive formulas as exponential moving averages. Those can save you time, and treat borders with less pain.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 1, "mathjax_display_tex": 1, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 1, "x-ck12": 0, "texerror": 0, "math_score": 0.7589625120162964, "perplexity": 860.7393041972834}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-10/segments/1614178365454.63/warc/CC-MAIN-20210303042832-20210303072832-00130.warc.gz"}