# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import sys
import os.path
import numpy as np
import pandas as pd
import wave
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
from matplotlib.backends.backend_pdf import PdfPages
from collections import OrderedDict
from scipy import signal
from scipy.optimize import curve_fit 
from functools import wraps
from numba import jit


# ディレクトリの絶対パスを取得
current_dir = os.path.dirname(os.path.abspath("__file__"))
# モジュールのあるパスを追加
sys.path.append( str(current_dir) + '/../../programs' )

get_ipython().run_line_magic('matplotlib', 'inline')
# %matplotlib inline

plt.rcParams['font.family'] ='IPAPGothic' #使用するフォント
plt.rcParams['xtick.direction'] = 'in' #x軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
plt.rcParams['ytick.direction'] = 'in' #y軸の目盛線が内向き('in')か外向き('out')か双方向か('inout')
plt.rcParams['xtick.top'] = True #x軸の目盛線の上側を表示
plt.rcParams['ytick.right'] = True #y軸の目盛線の右側を表示
plt.rcParams['xtick.major.width'] = 1.0 #x軸主目盛り線の線幅
plt.rcParams['ytick.major.width'] = 1.0 #y軸主目盛り線の線幅
plt.rcParams['font.size'] = 11 #フォントの大きさ
plt.rcParams['axes.linewidth'] = 1.0 #軸の線幅edge linewidth。囲みの太さ
plt.rcParams['figure.figsize'] = (7,5)
plt.rcParams['figure.dpi'] = 100 #dpiの設定
plt.rcParams['figure.subplot.hspace'] = 0.3 # 図と図の幅
plt.rcParams['figure.subplot.wspace'] = 0.3 # 図と図の幅

fig = plt.figure(figsize=(8, 11))
# -

import decorators
import plot_tool
import research_tools


def make_random_noise():
    global N            # サンプル数
    global fs
    global dt
    global f1, f2, f3
    global t
    
    noise = 0.5*np.random.randn(N)
    x = 3*np.sin(2*np.pi*f1*t) + 0.3*np.sin(2*np.pi*f2*t) + 0.2*np.sin(2*np.pi*f3*t) + noise# 信号
    return x



# +
N = 4096            # サンプル数
fs = 1024
dt = 1/fs            # サンプリング周期 [s]
f1, f2, f3 = 10, 60, 300 # 周波数 [Hz]

t = np.arange(0, N*dt, dt) # 時間 [s]
# -

test_signal = make_random_noise()

test_signal

# LPF設計
lpf_cutoff_hz = 200.0
lpf_cutoff = lpf_cutoff_hz / (fs/2.0)
win = "hamm"
lpf = signal.firwin(N, lpf_cutoff, window=win)


# + code_folding=[]
# 線形フィルタ適用
@decorators.stop_watch
def filter_on_t(data):
    ys = signal.lfilter(lpf, [1.0], data)
    return ys


# -

# 周波数領域フィルタ適用
@decorators.stop_watch
def filter_on_f(data):
    zs = signal.fftconvolve(data, lpf, mode="same")
    return zs


ys = filter_on_t(test_signal)

zs = filter_on_f(test_signal)

plot_tool.plot_3charts(N=N, y=test_signal, fs=fs)

plot_tool.plot_3charts(N=N, y=ys, fs=fs)

plot_tool.plot_3charts(N=N, y=zs, fs=fs)

print(plot_tool.plot_3charts.__doc__)


# ### FFTの長さNが長いときは時間領域の畳込みよりも周波数領域の掛け算のほうが早い

@decorators.stop_watch
def rand_append():
    a = []
    for i in range(100000000):
        a.append(i)
        
    return a


@decorators.stop_watch
def rand_listin():
    a = [i for i in range(100000000)]
    return a


a = rand_append()

a = rand_listin()


class ref_speed_test(object):
    def __init__(self, a=5):
        self.a = 1 / a


A = ref_speed_test(a=5)


@decorators.stop_watch
def print_propaty(num):
    for _ in range(10000000):
        x = A.a
    print(x)


@decorators.stop_watch
def print_args(num):
    for _ in range(10000000):
        x = 1 / num
    print(x)


print_propaty(8)

print_args(5)



StarcoderdataJupyterScriptsDedupFiltered