adding noise to a signal in python

前端 未结 7 747
你的背包
你的背包 2020-12-12 09:55

I want to add some random noise to some 100 bin signal that I am simulating in Python - to make it more realistic.

On a basic level, my first thought was to go bin b

7条回答
  •  臣服心动
    2020-12-12 10:30

    Awesome answers above. I recently had a need to generate simulated data and this is what I landed up using. Sharing in-case helpful to others as well,

    import logging
    __name__ = "DataSimulator"
    logging.basicConfig(level=logging.INFO)
    logger = logging.getLogger(__name__)
    
    import numpy as np
    import pandas as pd
    
    def generate_simulated_data(add_anomalies:bool=True, random_state:int=42):
        rnd_state = np.random.RandomState(random_state)
        time = np.linspace(0, 200, num=2000)
        pure = 20*np.sin(time/(2*np.pi))
    
        # concatenate on the second axis; this will allow us to mix different data 
        # distribution
        data = np.c_[pure]
        mu = np.mean(data)
        sd = np.std(data)
        logger.info(f"Data shape : {data.shape}. mu: {mu} with sd: {sd}")
        data_df = pd.DataFrame(data, columns=['Value'])
        data_df['Index'] = data_df.index.values
    
        # Adding gaussian jitter
        jitter = 0.3*rnd_state.normal(mu, sd, size=data_df.shape[0])
        data_df['with_jitter'] = data_df['Value'] + jitter
    
        index_further_away = None
        if add_anomalies:
            # As per the 68-95-99.7 rule(also known as the empirical rule) mu+-2*sd 
            # covers 95.4% of the dataset.
            # Since, anomalies are considered to be rare and typically within the 
            # 5-10% of the data; this filtering
            # technique might work 
            #for us(https://en.wikipedia.org/wiki/68%E2%80%9395%E2%80%9399.7_rule)
            indexes_furhter_away = np.where(np.abs(data_df['with_jitter']) > (mu + 
             2*sd))[0]
            logger.info(f"Number of points further away : 
            {len(indexes_furhter_away)}. Indexes: {indexes_furhter_away}")
            # Generate a point uniformly and embed it into the dataset
            random = rnd_state.uniform(0, 5, 1)
            data_df.loc[indexes_furhter_away, 'with_jitter'] +=  
            random*data_df.loc[indexes_furhter_away, 'with_jitter']
        return data_df, indexes_furhter_away
    

提交回复
热议问题