How can generate impulse as outliers on periodic or sequenced-based data for doing experiments via outliers detection methods?

后端 未结 3 1981
温柔的废话
温柔的废话 2021-01-23 03:12

I want to carry out some experiments on some time-series data with the KM approach has been suggested in this paper. The problem is I don\'t access the data in the paper but I h

3条回答
  •  温柔的废话
    2021-01-23 03:42

    You can generate a signal with eg.: numpy Python module. And you can add your impulses to this signal (of course, if the dimensions are correct). I have written an example for your where I have generated a Sinus signal with numpy and I have added the impulses with the signal.unit_impulse() like in your question. I have added several comments to code for the better understanding.

    Code:

    import numpy as np
    import matplotlib.pyplot as plt
    import scipy.signal as signal
    
    positive_impulses = signal.unit_impulse(200, [10, 50, 60])  # Generate positive impulses
    negative_impulses = signal.unit_impulse(200, [80, 100, 150])*-1  # Generate negative impulses
    
    # Generate the Sinus signal.
    t = np.linspace(1, 200, 200)
    x_sin_sig = np.sin(t / (2 * np.pi))
    plt.subplot(4, 1, 1)
    plt.plot(t, x_sin_sig)
    plt.title("Signal")
    plt.ylabel("Sin")
    
    
    plt.subplot(4, 1, 2)
    plt.plot(t, x_sin_sig + positive_impulses)  # Add the positive impulses to the original signal
    plt.title("Signal with positive impulses")
    
    plt.subplot(4, 1, 3)
    plt.plot(t, x_sin_sig + negative_impulses)  # Add the negative impulses to the original signal
    plt.title("Signal with negative impulses")
    
    plt.subplot(4, 1, 4)
    plt.plot(t, x_sin_sig + positive_impulses + negative_impulses)  # Add the both impulses to the original signal
    plt.title("Signal with different impulses")
    
    plt.tight_layout()
    plt.show()
    

    Output:

    Note:

    Probably more efficient to generate random impulses for your signal. You can do it with the random module.

    import random
    
    positive_impulses = signal.unit_impulse(200, random.sample(range(200), 5))  # Generate positive impulses
    negative_impulses = signal.unit_impulse(200, random.sample(range(200), 5))*-1  # Generate negative impulses
    

    The random.sample(range(200), 5) will return a list of 5 numbers selected from the range 0 to 200, without duplicates.

    An example with random impulses:

提交回复
热议问题