メインコンテンツまでスキップ

「Math」タグの記事が4件件あります

全てのタグを見る

0.248 (16) を 10 進数の分数にする問題の簡単な解き方 (基本情報技術者試験)

· 約1分
ひかり
Main bloger

「16 進数の少数 0.248 を 10 進数の分数で表したものはどれか。」の問題について簡単に解く方法を紹介します。

ポイント

16 進数の問題は 2 進数を使う

手順

  1. 16 進数を 2 進数に変換
    0.248(16) = 0.0010 0100 1000(2)

  2. 小数点をなくす
    0.0010 0100 1000(2) = 0.0010 0100 1000(2) × 0010 0000 0000(2) / 0010 0000 0000(2) = 0100 1001(2) / 0010 0000 0000(2)

  3. 2 進数を 10 進数に変換
    0100 1001(2) / 0010 0000 0000(2) = (64 + 8 + 1) / 512
    = 73 / 512

Python で極値の検出

· 約2分
ひかり
Main bloger

概要

信号の極値を検出する。

信号の生成

例として疑似的な信号を生成する。

  • 3 [Hz] の信号 + 0.01 [Hz] の信号 + ノイズ
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy import signal

t = np.arange(30 * 100) / 100
x = np.sin(2 * np.pi * t / 3) + np.random.randn(len(t)) * 0.2 + np.sin(2 * np.pi * t / 100)
df = pd.DataFrame({'Data': x}, index=pd.to_datetime(t, unit='s').time)

fig, ax = plt.subplots()
df.plot(ax=ax, xlim=['00:00:00', '00:00:10'])
plt.show()

tmp

信号の性質を調べる

def acorr(df: pd.DataFrame, ra: int = 3, fs=100):
x = np.correlate(df.Data.values, df.Data.values, mode='full')
t = (np.arange(len(x)) - len(x) / 2 + 0.5) / fs
x /= x.max()
fig, ax = plt.subplots()
ax.set_xlim(-ra, ra)
ax.set_ylim(0, 1)
ax.plot(t, x)
ax.set_title('Autocorrelation')
ax.grid(axis='x')
ax.set_xticks(np.linspace(-ra, ra, 2 * ra + 1))
plt.show()

acorr(df)

主な信号は 1/3 [Hz] であることがわかる。そのため、1/3 [Hz] でローパスフィルターをかける。

tmp

ローパスフィルターをかけ、極値の検出

ローパスフィルターをかけ、極値を検出できるようにする。

1/3 [Hz] でローパスフィルターをかけても正しく検出されない場合は、 周波数を下げていく。

tmp

fs = 10
lpf = 0.1
b, a = signal.butter(5, lpf / fs * 2, 'low')
df['Filter'] = signal.filtfilt(b, a, df.Data.values)
max_idx = signal.argrelmax(df['Filter'].values)
min_idx = signal.argrelmin(df['Filter'].values)

df['max_index'] = False
df.iloc[max_idx[0], 2] = True
df['min_index'] = False
df.iloc[min_idx[0], 3] = True

fig, ax = plt.subplots()
df.plot(ax=ax)
ax.set_xlim(['00:00:00', '00:00:30'])
ax.scatter(
df.loc[df['max_index'], ['Filter']].index,
df.loc[df['max_index'], ['Filter']],
color='tab:orange',
zorder=3)
ax.scatter(
df.loc[df['min_index'], ['Filter']].index,
df.loc[df['min_index'], ['Filter']],
color='tab:green',
zorder=3)
plt.show()

NMF (HALS) の実装

· 約1分
ひかり
Main bloger
import numpy as np

X = np.array([[1, 1], [2, 1], [3, 1.2], [4, 1], [5, 0.8], [6, 1]])

n_components, n_samples, n_features, = (2,) + X.shape
W = np.random.uniform(size = (n_samples, n_components))
H = np.random.uniform(size = (n_components, n_features))

eps = 1e-4

# NMF
for i in range(100):
# update B
A = X.T.dot(W)
B = W.T.dot(W)
for j in range(n_components):
tmp = H[j, :] + A[:, j] - H.T.dot(B[:, j])
H[j, :] = np.maximum(tmp, eps)

# update A
C = X.dot(H.T)
D = H.dot(H.T)
for j in range(n_components):
tmp = W[:, j] * D[j, j] + C[:, j] - W.dot(D[:, j])
W[:, j] = np.maximum(tmp, eps)
norm = np.linalg.norm(W[:, j])
if norm > 0:
W[:, j] /= norm

print(W)
print(H)
print(W.dot(H))

Bash でユークリッド互除法

· 約1分
ひかり
Main bloger
  1. A と B の余り C

  2. B と C の余り D

  3. C と D の余り E

    ...

  4. X と Y の余り 0

    となったときの Y の値が最大公約数。

Bash のスクリプト

#!/usr/bin/env bash

function gcd(){
test $2 -eq 0 && echo $1 || gcd $2 $(($1 % $2))
}

gcd 1071 1029
# 21