Search code examples
pythonmatplotlibsignal-processingscaling

How to horizontally stretch a signal


I have a dataframe with two columns. This is an example of signal that varies along depth:

import matplotlib.pyplot as plt 
import pandas as pd

df = pd.DataFrame({"DEPTH":[5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                   "PROPERTY":[23, 29, 26, 17, 15, 20, 28, 25, 20, 17]})

plt.figure()
plt.plot(df["PROPERTY"],
         df["DEPTH"])
plt.xlabel("PROPERTY")
plt.ylabel("DEPTH")
plt.gca().invert_yaxis()
plt.grid()

enter image description here

But I would like to stretch my signal horizontally so I could see it more "wide". In my real data, my signal is smoothed (almost flat). For example, high values should be higher and low ones should be lower, like this new orange signal:

enter image description here

Does anyone know how I could do it? If I multiply by a number, all the values would be higher.


Solution

  • This code will stretch the signal horizontally by the scale factor exaggeration.

    It works by centering the sigal at 0, amplifying the peaks/troughs using the desired scale factor, and then adding the mean back in.

    enter image description here

    import matplotlib.pyplot as plt 
    import pandas as pd
    
    df = pd.DataFrame({"DEPTH":[5, 10, 15, 20, 25, 30, 35, 40, 45, 50],
                       "PROPERTY":[23, 29, 26, 17, 15, 20, 28, 25, 20, 17]})
    exaggeration = 5
    plt.figure()
    plt.plot(df["PROPERTY"], df["DEPTH"], label='original')
    plt.plot(exaggeration * (df["PROPERTY"] - df.PROPERTY.mean()) + df.PROPERTY.mean(),
             df["DEPTH"], label=f'exaggerated {exaggeration}x')
    plt.xlabel("PROPERTY")
    plt.ylabel("DEPTH")
    plt.gca().invert_yaxis()
    plt.grid()
    plt.legend()