# How to calculate the real cepstrums of a time-series in Julia?

**URL:** https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169
**Category:** Signal and Image Processing
**Created:** [January 23, 2024, 7:45pm UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169 "2024-01-23T19:45:30Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [January 23, 2024, 7:45pm UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/1 "2024-01-23T19:45:30Z")

</div>

That being said, the real cepstrum of a time series can be calculated by

> the inverse Fourier transform of the real logarithm of the Fourier transform of the time–series ([K. Kalpakis et al., 2001](https://doi.org/10.1109/ICDM.2001.989529)).

Let’s assume that we have the following time series:

```julia
using YFinance
series = get_prices("MSFT", startdt="2021-01-01", enddt="2021-01-20")["adjclose"]
# 11-element Vector{Float64}:
# 211.99659729003906
# 212.20114135742188
# 206.69886779785156
# 212.5809326171875
# 213.87611389160156
# 211.80184936523438
# 209.30877685546875
# 210.6819305419922
# 207.44874572753906
# 207.0884246826172
# 210.779296875

```

Now, I want to calculate the real cepstrum of this time series. According to the definition brought earlier, the following steps should be taken:

1. Calculate the Fourier transform of the time series.
2. Calculate the real logarithm of the result of 1st step.
3. Take the inverse Fourier transform of the result of 2nd step.

So, the process can be fused as follows:

```julia
using FFTW
series |> fft .|> real .|> log |> ifft
# ERROR: DomainError with -2.1316965395989245:
# log was called with a negative real argument but will only return a complex result if called with a complex argument. Try log(Complex(x)).

```

So, there is a mathematical problem that can be handled as mentioned in the description of the error:

```julia
  series |> fft .|> real .|> Complex .|> log |> ifft
  # 11-element Vector{ComplexF64}:
  # 2.1053213005396554 + 1.71359599286716im
  # 0.5250627554737464 - 0.4415937871666108im
  # 0.3054836751101531 + 0.6365173493037021im
  # 0.5006638565965567 + 0.025177272226508857im
  # 0.8459161100203939 - 0.6848321808959316im
  # 0.643679369835847 - 0.3920666499012487im
  # 0.643679369835847 - 0.3920666499012487im
  # 0.8459161100203939 - 0.6848321808959316im
  # 0.5006638565965567 + 0.025177272226508857im
  # 0.3054836751101531 + 0.6365173493037021im
  # 0.5250627554737464 - 0.4415937871666108im

```

However, I have a series of Complex values. Then, what are the real cepstrums? Are they the real part of the values? Although, these questions are valid _if_ I have done the procedure correctly.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 24, 2024, 3:34am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/2 "2024-01-24T03:34:54Z")

</div>

The function [`mfcc`](https://github.com/baggepinnen/LPVSpectral.jl/blob/master/src/mel.jl#L136C1-L161C4) in [GitHub - baggepinnen/LPVSpectral.jl: Least-squares (sparse) spectral estimation and (sparse) LPV spectral decomposition.](https://github.com/baggepinnen/LPVSpectral.jl) ?

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [January 24, 2024, 5:36am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/3 "2024-01-24T05:36:10Z")

</div>

Hey, thank you for your reply.  
Unfortunately, that’s not the case since it’s about Mel Frequency Cepstral Coefficients, a different concept.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 24, 2024, 5:41am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/4 "2024-01-24T05:41:26Z")

</div>

It’s not that different, the `mfcc` computes the real cepstrum over time, scaled using the Mel frequency scaling. Using a single bin over time, you would essentially get the real cepstrum, apart from the frequency scaling.

* * *

Actually, `mfcc` computes the power spectrum rather than the real spectrum, so they are slightly more different than I first wrote.

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [January 24, 2024, 6:45am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/5 "2024-01-24T06:45:54Z")

</div>

Yes Mr. @baggepinnen,  
I wish we would have a specific library for this in Julia. I’m not a signal-processing expert. Otherwise, I would have tried to write a package in this field. I wonder why the Mel Frequency Cepstrum is that popular among the community, and people barely touch Real Cepstrum. I didn’t find any implementation in this regard. However, there are dozens of Mel… Implementations in various languages.

---

<div class="post-metadata">

### Author: ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)
#### Post date: [January 24, 2024, 7:14am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/6 "2024-01-24T07:14:19Z")

</div>

> [@Shayan](#):
>
> I wonder why the Mel Frequency Cepstrum is that popular among the community

It’s useful for audio processing. The Mel frequency scaling is constructed to mimic how humans hear, making it particularly suitable for working with spoken language etc.

---

<div class="post-metadata">

### Author: ![Shayan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shayan/32/33025_2.png) [@Shayan](https://discourse.julialang.org/u/Shayan)
#### Post date: [January 27, 2024, 10:01am UTC](https://discourse.julialang.org/t/how-to-calculate-the-real-cepstrums-of-a-time-series-in-julia/109169/7 "2024-01-27T10:01:25Z")

</div>

> [@Shayan](#):
>
> `series |> fft .|> real .|> Complex .|> log |> ifft`

I got it. I need:

```julia
series |> fft .|> abs .|> log |> ifft
# 11-element Vector{ComplexF64}:
# 2.513777018119395 + 0.0im
# 0.6631478310353252 + 0.0im
# 0.4305484020274489 + 0.0im
# 0.44641736254372333 + 0.0im
# 0.63729469653841 + 0.0im
# 0.43916962928751924 + 0.0im
# 0.43916962928751924 + 0.0im
# 0.63729469653841 + 0.0im
# 0.44641736254372333 + 0.0im
# 0.4305484020274489 + 0.0im
# 0.6631478310353252 + 0.0im

```

And they do not have imaginary parts, actually.
