这个就是我目前已经实现的,对11种调制信号进行分类识别。
然后我想后续做一个开集识别,就是当出现除了这11种信号以外的,能够识别出来它不在11种里面就可以了。
#!/usr/bin/env python
# encoding: utf-8
import pickle
import numpy as np
import time
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from pytorchtools import EarlyStopping
from ge2e import GE2ELoss
from torch.optim import SGD
import torch
import torch.utils.data as Data
import torch.nn as nn
from sklearn.metrics import accuracy_score, confusion_matrix
# 调制信号类别
classes = {
b'QAM16': 0, b'QAM64': 1, b'8PSK': 2, b'WBFM': 3, b'BPSK': 4,
b'CPFSK': 5, b'AM-DSB': 6, b'GFSK': 7, b'PAM4': 8, b'QPSK': 9, b'AM-SSB': 10}
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
snrs = ""
mods = ""
def data_process(fp):
global snrs, mods, lbl
Xd = pickle.load(open(fp, 'rb'), encoding='bytes') # 读取数据
'''
从数据集中遍历返回snrs,mods两个参数. 1,0是j的参数
map会根据提供的函数对指定序列做映射。lambda 表达式 冒号左边是函数的参数,右边是函数的返回值(逻辑表达式)
map(lambda x: x[j], Xd.keys())#Xd.key()取出key,x[key]再返回
Xd.keys()取出Xd中的键keys,形为('8PSK',-10),
故snrs值为:sorted(list(set(map(lambda x: x[1], Xd.keys())))),
mods值为:sorted(list(set(map(lambda x: x[0], Xd.keys()))))
'''
snrs, mods = map(lambda j: sorted(
list(set(map(lambda x: x[j], Xd.keys())))), [1, 0])
X = []
y = []
lbl = []
for mod in mods:
for snr in snrs:
X.append(Xd[(mod, snr)]) # X将Xd开头的(mod,snrs)去掉后的矩阵
for i in range(Xd[(mod, snr)].shape[0]):
y.append(mod) # lpl:(b'8PSK',-20)... len:220000
lbl.append(snr)
X = np.vstack(X) # 垂直(按照行顺序)的把数组给堆叠起来。(220000,2,128)
Y = [classes[yy] for yy in y]
Y = np.array(Y, dtype=np.int64) # list转换成int64
Y = torch.from_numpy(Y) # 数据的类别标签要转化为int64
return (X, Y)
def train_test_split(X, Y, test_size, random_state):
global test_idx
np.random.seed(random_state)
n_examples = X.shape[0]
n_train = int(n_examples * (1-test_size))
train_idx = np.random.choice(
range(0, n_examples), size=n_train, replace=False)
test_idx = list(set(range(0, n_examples)) - set(train_idx))
X_train = X[train_idx]
Y_train = Y[train_idx]
X_test = X[test_idx]
Y_test = Y[test_idx]
return (X_train, X_test, Y_train, Y_test)
#
def to_transpose(X_train, X_test, nsamples):
X_train_cmplx = X_train[:, 0, :] + 1j * X_train[:, 1, :]
X_test_cmplx = X_test[:, 0, :] + 1j * X_test[:, 1, :]
X_train_amp = np.abs(X_train_cmplx)
X_train_ang = np.arctan2(X_train[:, 1, :], X_train[:, 0, :]) / np