0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonで時刻歴データ内の繰り返し部分を抽出する

Posted at

1.時刻歴データから繰り返し部分を自動で抽出したい
2.抽出したデータを正規化して,オーバープロットしたい

時刻歴データサンプル
スクリーンショット 2024-11-07 7.12.04.jpg

サンプルコードを示す.

extract_data.py
# -*- coding: utf-8 -*-
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

def extract_and_normalize_patterns(data, num_patterns=5, threshold=0.5):
    # 自己相関関数を計算
    autocorr = np.correlate(data, data, mode='full')
    autocorr = autocorr[len(autocorr)//2:]
    
    # ピークを検出
    peaks, _ = find_peaks(autocorr, height=threshold*np.max(autocorr))
    
    if len(peaks) < 2:
        return []
    
    # 最初の2つのピーク間の距離を周期として使用
    period = peaks[1] - peaks[0]
    
    # 指定された数の繰り返しパターンを抽出
    patterns = []
    for i in range(num_patterns):
        start = i * period
        end = start + period
        if end > len(data):
            break
        pattern = data[start:end]
        
        # パターンを正規化
        normalized_pattern = (pattern - np.min(pattern)) / (np.max(pattern) - np.min(pattern))
        patterns.append(normalized_pattern)
    
    return patterns

# サンプルデータの生成(実際のデータに置き換えてください)
t = np.linspace(0, 20, 2000)
data = np.sin(2*np.pi*t) + 0.1*np.random.randn(len(t))

plt.figure(figsize=(12, 6))
plt.plot(t, data)
plt.title('Original Data')
plt.xlabel('Time')
plt.ylabel('Amplitude')
plt.show()
# 繰り返しパターンの抽出と正規化
normalized_patterns = extract_and_normalize_patterns(data, num_patterns=10)

if normalized_patterns:
    # 結果のプロット
    plt.figure(figsize=(12, 6))
    normalized_t = np.linspace(0, 1, len(normalized_patterns[0]))
    
    for i, pattern in enumerate(normalized_patterns):
        plt.plot(normalized_t, pattern, label=f'Pattern {i+1}', alpha=0.7)
    
    plt.title('Normalized Repetitive Patterns')
    plt.xlabel('Normalized Time')
    plt.ylabel('Normalized Amplitude')
    plt.legend()
    plt.grid(True)
    plt.show()
else:
    print("No repetitive patterns found.")

スクリーンショット 2024-11-07 7.12.52.jpg

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?