File size: 3,822 Bytes
6fa23b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# coding:utf-8
"""
name        : dataset.py,
version     : 1.0.0,
url         : https://github.com/abubakar-sani/RFF,
license     : MIT License,
copyright   : Copyright 2021 by Abubakar Sani Ali, Khalifa University,
author      : Abubakar Sani Ali,
email       : [email protected],
date        : 9/5/2022,
description : Dataset module that returns the processed data,
"""

import math
import os
import numpy as np
import pandas as pd

import json

base_dir = os.path.abspath('')
raw_dir = f'{base_dir}/datasets/raw/spectral_scans_QC9880_ht20_background'
processed_dir = f'{base_dir}/datasets/processed/spectral_scans_QC9880_ht20_background'
    
    
def load_spectral_scans(jammer, jammed_freq, jammer_dist, jamming_power, channels, n_features, n_scans=10):
    if jammer == 'combined':
        scenario = f'samples_chamber_{jammer_dist}cm_{jamming_power}dBm'
    elif jammer == 'none':
        interference = np.random.choice(['high', 'medium', 'low'], p=[0.4, 0.4, 0.2])
        if interference == 'high':
            scenario = f'samples_office_None'
        elif interference == 'low':
            scenario = f'samples_lab_None'
        else:
            scenario = f'samples_chamber_None'
    else:
        scenario = f'samples_chamber_{jammed_freq}MHz_{jammer_dist}cm_{jamming_power}dBm'
    # Process the dataset and generate the raw numpy (to processing it every run)
    if not os.path.isfile(f'{processed_dir}/{scenario}.npy'):
        spectral_data = []
        for channel in channels:
            channel_data = np.empty((0, n_features), int)
            for scan in range(n_scans):
                if jammer == 'combined':
                    scenario = f'samples_chamber_{channel}MHz_{jammer_dist}cm_{jamming_power}dBm'

                df = pd.read_csv(f'{raw_dir}/{scenario}_{scan}.csv')
                temp_data = df.where(df['freq1'] == channel).dropna()
                temp_data = temp_data.to_numpy()
                channel_data = np.append(channel_data, temp_data, axis=0)

            spectral_data.append(channel_data)

        if not os.path.exists(processed_dir):
            os.makedirs(processed_dir)
        if jammer == 'combined':
            scenario = f'samples_chamber_{jammer_dist}cm_{jamming_power}dBm'
        np.save(f'{processed_dir}/{scenario}', np.array(spectral_data, dtype='object'))

    spectral_data = np.load(f'{processed_dir}/{scenario}.npy', allow_pickle=True)

    return spectral_data


def get_feats_dirs(scenario, scan=1):
    spectral_df = pd.read_csv(f'{raw_dir}/{scenario}_{scan}.csv')
    features = spectral_df.columns
    n_features = len(features)
    return features, n_features


def process_data(data, channels, length, stride, time_step, mode=0):
    if mode == 0:
        min_len = len(data[0])
        for i in range(len(data)):
            min_len = len(data[i]) if len(data[i]) < min_len else min_len
        t_samples = min_len
    else:
        t_samples = len(data)
    n_samples = math.floor((t_samples - length) / stride)
    if mode == 1:
        # Breaking data into batches
        data = data[0:t_samples]
        batches = []
        for sample in range(n_samples):
            batch = []
            for i in range(length):
                batch_sample = data[(sample * stride) + i]
                batch.append(batch_sample)
            batches.append(batch)

    else:
        batches = []
        for sample in range(n_samples):
            batch = []
            for i in range(len(channels)):
                channel_data = data[i]
                channel_samples = channel_data[(sample * stride):(length + (sample * stride)), :]
                batch.append(channel_samples)
            batches.append(batch)

    processed_data = np.array(batches[time_step % len(batches)])

    return processed_data