前言:
感知行为分为下面7种
bed |
fall |
pickup |
run |
sitdown |
stand up |
walk |
论文地址:
本篇重点了解一下数据集
- 数据集说明
- np.convolve
- STFT 算法
一 数据集说明:
数据集下载地址: https://drive.google.com/file/d/19uH0_z1MBLtmMLh8L4BlNA0w-XAFKipM/view
1.1 标签 Y
annotation_ 开头的 是标签,主要研究方向分为下面几种行为
["bed", "fall","pickup","run","sitdown","standup","walk"]
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 14:25:27 2024
@author: chengxf2
"""
import os
dirPath = "D:\AI\Wifi_Activity_Recognition-master\Dataset\Data"
file_name = os.listdir(dirPath)
sets = set()
for file in file_name:
substring = file[:10]
#print(substring)
if substring =="annotation":
subs = file.split("_")
#print(subs[1])
sets.add(subs[1])
print(sets)
2: 输入 X
The files with "input_" prefix are WiFi Channel State Information data.
-> 1st column shows timestamp. 第一列时间
-> 2nd - 91st column shows (30 subcarrier * 3 antenna) amplitude. 2-91 列3Rx对应的幅度
-> 92nd - 181st column shows (30 subcarrier * 3 antenna) phase.92-181 列对应相位
通过下面代码可以深入的了解数据集
每个表格包含20s 左右的数据集,1000行一秒
1: 显示不同Rx 对应的幅度
2: 对信号做卷积,平滑
3: PCA 降维: 显示最大特征值对应的6个主成信息
4: 对PCA 降维后的数据,做STFT 变换,观测其时频域信息
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 17:24:28 2024
@author: chengxf2
"""
import numpy as np
np.set_printoptions(threshold='nan')
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
def moving_average(data, window_size):
window= np.ones(int(window_size))/float(window_size)
return np.convolve(data, window, 'same')
def visualize(path1):
#data import
data = pd.read_csv(path1, header=None).values
amp = data[:,1:91]
#plt
fig = plt.figure(figsize = (18,10))
ax1 = plt.subplot(311)
plt.imshow(amp[:,0:29].T,interpolation = "nearest", aspect = "auto", cmap="jet")
ax1.set_title("Antenna1 Amplitude")
plt.colorbar()
ax2 = plt.subplot(312)
plt.imshow(amp[:,30:59].T,interpolation = "nearest", aspect = "auto", cmap="jet")
ax2.set_title("Antenna2 Amplitude")
plt.colorbar()
ax3 = plt.subplot(313)
plt.imshow(amp[:,60:89].T,interpolation = "nearest", aspect = "auto", cmap="jet")
ax3.set_title("Antenna3 Amplitude")
plt.colorbar()
plt.show()
# Initializing valiables
constant_offset = np.empty_like(amp)
filtered_data = np.empty_like(amp)
# Calculating the constant offset (moving average 4 seconds)
for i in range(1, len(amp[0])):
constant_offset[:,i] = moving_average(amp[:,i], 4000)
# Calculating the filtered data (substract the constant offset)
filtered_data = amp - constant_offset
# Smoothing (moving average 0.01 seconds)
for i in range(1, len(amp[0])):
filtered_data[:,i] = moving_average(filtered_data[:,i], 10)
# Calculate correlation matrix (90 * 90 dim)
cov_mat2 = np.cov(filtered_data.T)
# Calculate eig_val & eig_vec
eig_val2, eig_vec2 = np.linalg.eig(cov_mat2)
# Sort the eig_val & eig_vec
idx = eig_val2.argsort()[::-1]
eig_val2 = eig_val2[idx]
eig_vec2 = eig_vec2[:,idx]
# Calculate H * eig_vec
pca_data2 = filtered_data.dot(eig_vec2)
xmin = 0
xmax = 20000
# plt
fig3 = plt.figure(figsize = (18,20))
ax1 = plt.subplot(611)
plt.plot(pca_data2[xmin:xmax,0])
#plt.plot(pca_data2[2500:17500,0])
ax1.set_title("PCA 1st component")
ax2 = plt.subplot(612)
plt.plot(pca_data2[xmin:xmax,1])
#plt.plot(pca_data2[2500:17500,1])
ax2.set_title("PCA 2nd component")
ax3 = plt.subplot(613)
plt.plot(pca_data2[xmin:xmax,2])
#plt.plot(pca_data2[2500:17500,2])
ax3.set_title("PCA 3rd component")
ax4 = plt.subplot(614)
plt.plot(pca_data2[xmin:xmax,3])
#plt.plot(pca_data2[2500:17500,3])
ax4.set_title("PCA 4th component")
ax5 = plt.subplot(615)
plt.plot(pca_data2[xmin:xmax,4])
#plt.plot(pca_data2[2500:17500,4])
ax5.set_title("PCA 5th component")
ax6 = plt.subplot(616)
plt.plot(pca_data2[xmin:xmax,5])
#plt.plot(pca_data2[2500:17500,5])
ax6.set_title("PCA 6th component")
plt.show()
plt.figure(figsize = (18,30))
# Spectrogram(STFT)
plt.subplot(611)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,0], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.subplot(612)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,1], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.subplot(613)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,2], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.subplot(614)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,3], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.subplot(615)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,4], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.subplot(616)
Pxx, freqs, bins, im = plt.specgram(pca_data2[:,5], NFFT=128, Fs=1000, noverlap=1, cmap="jet", vmin=-100,vmax=20)
plt.xlabel("Time[s]")
plt.ylabel("Frequency [Hz]")
plt.title("Spectrogram(STFT)")
plt.colorbar(im)
plt.xlim(0,10)
plt.ylim(0,100)
plt.show()
plt.figure(figsize = (18,10))
ax = plt.subplot(111)
# ax.magnitude_spectrum(pca_data2[:,0], Fs=1000, scale='dB', color='C1')
ax.magnitude_spectrum(pca_data2[5000:7500,0], Fs=1000, color='C1')
plt.xlim(0,100)
plt.ylim(0,1000)
plt.show()
visualize(path1 = "170401_activity_data_UABC_L2_building_2_LOS\Input\input_fall_170310_1136_01.csv")
二 np.convolve(x,h,mode)
卷积,加权平滑 特征提取.
2.1 输入:
x: 输入信号
h: 卷积核
mode: 模式
2.2 原理:
主要分为两步:
1 对h(t) reverse
2 h(t) 向右平移,和x(t) 重叠的窗口 相乘 求和
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 18 20:47:43 2024
@author: cxf
"""
import numpy as np
#signal
x = [1,2,3,4]
h =[4,3,2]
out =np.convolve(x,h,'full')
print(out)
上面是full mode 模式,
如果是same 模式,就是默认从中间取max(M,N)
三 STFT
3 .1 简介
3.2 数学定义
上面对应傅里叶变换
下面对应STFT 短时傅里叶变换
3.3 分帧原理
3.4 API 实现
specgram(signal,N,fs,window,overlap):绘制语谱图,其中,
参数:
x:1-D数组或序列
Fs:采样频率,默认为2
NFFT:FFT中每个片段的数据点数(窗长度)。默认为256
noverlap:窗之间的重叠长度。默认值是128。取50%
step = NFFT-noverlap
noverlap : 块之间的重叠点数。 default: 128。
mode: {‘default’, ‘psd’, ‘magnitude’, ‘angle’, ‘phase’}
%
Spectrogram是基于STFT变换得到的,非常有助于分析信号的时频特性,在语音信号处理中常被称为"语谱图"。
为什么要用STFT: 参考下面
Python实现短时傅里叶变换(STFT)的3d动画展示_哔哩哔哩_bilibili
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 16:03:11 2024
@author: cxf
"""
import numpy as np
import matplotlib.pyplot as plt
'''
signal : 输入信号
fs: 采样频率
NFFT: FFT 窗口大小
noverlap: 重叠窗口
FS: 采样频率
返回:
Pxx: 频率密度
freqs: 频率数组
bins: 时间数组
'''
noverlap= None
#采样频率
fs =512
t = np.linspace(0,1,fs)
signal = np.sin(2*np.pi*100*t)+np.sin(2*np.pi*200*t)
#帧长,重叠长度
frame_size,overlap =256,128
#帧移
hop_size = frame_size-overlap
N = len(signal)
frame_num = (N-overlap)/hop_size
print("\n frame_num ",frame_num)
print(frame_size, overlap)
'''
powers is the segments x freqs array of instantaneous power
freqs is the frequency vector,
bins are the centers of the time bins in which the power is computed,
im is the matplotlib.image.AxesImage
# instance
x, NFFT=None, Fs=None, Fc=None, detrend=None, window=None,
noverlap=None, cmap=None, xextent=None, pad_to=None,
sides=None, scale_by_freq=None, mode=None, scale=None,
vmin=None, vmax=None, *, data=None, **kwargs):
'''
powers,freqs, bins, im =plt.specgram(signal,NFFT= frame_size,Fs=fs,noverlap=overlap)
print(np.shape(powers))
print(np.shape(freqs))
print(np.shape(bins))
plt.xlabel('Time ' )
plt.ylabel('Freq ' )
plt.colorbar(label='Intensity')
plt.show()
np.convolve(x,h, mode=‘##‘)的使用-CSDN博客