summaryrefslogtreecommitdiff
path: root/src/input-parser.cpp
blob: f9ad73b9b926f474f3c339d89ef1c8d36b57d16d (plain)
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (c) 2020-2021 Vasile Vilvoiu <vasi@vilvoiu.ro>
 *
 * specgram is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */
#include "input-parser.hpp"

#include <cassert>

InputParser::InputParser(double prescale, bool is_complex) : prescale_factor_(prescale), is_complex_(is_complex)
{
}

std::size_t
InputParser::GetBufferedValueCount() const
{
    return values_.size();
}

std::vector<Complex>
InputParser::PeekValues(std::size_t count) const
{
    count = std::min<std::size_t>(count, this->values_.size());
    if (count == 0) {
        return std::vector<Complex>();
    }
    return std::vector<Complex> (this->values_.begin(), this->values_.begin() + count);
}

void
InputParser::RemoveValues(std::size_t count)
{
    count = std::min<std::size_t>(count, this->values_.size());
    if (count > 0) {
        this->values_.erase(this->values_.begin(), this->values_.begin() + count);
    }
}

std::unique_ptr<InputParser>
InputParser::Build(DataType dtype, double prescale, bool is_complex)
{
    if (dtype == DataType::kSignedInt8) {
        return std::make_unique<IntegerInputParser<int8_t>>(prescale, is_complex);
    } else if (dtype == DataType::kSignedInt16) {
        return std::make_unique<IntegerInputParser<int16_t>>(prescale, is_complex);
    } else if (dtype == DataType::kSignedInt32) {
        return std::make_unique<IntegerInputParser<int32_t>>(prescale, is_complex);
    } else if (dtype == DataType::kSignedInt64) {
        return std::make_unique<IntegerInputParser<int64_t>>(prescale, is_complex);
    } else if (dtype == DataType::kUnsignedInt8) {
        return std::make_unique<IntegerInputParser<int8_t>>(prescale, is_complex);
    } else if (dtype == DataType::kUnsignedInt16) {
        return std::make_unique<IntegerInputParser<int16_t>>(prescale, is_complex);
    } else if (dtype == DataType::kUnsignedInt32) {
        return std::make_unique<IntegerInputParser<int32_t>>(prescale, is_complex);
    } else if (dtype == DataType::kUnsignedInt64) {
        return std::make_unique<IntegerInputParser<int64_t>>(prescale, is_complex);
    } else if (dtype == DataType::kFloat32) {
        return std::make_unique<FloatInputParser<float>>(prescale, is_complex);
    } else if (dtype == DataType::kFloat64) {
        return std::make_unique<FloatInputParser<double>>(prescale, is_complex);
    } else {
        throw std::runtime_error("unknown datatype");
    }
}

template <class T>
IntegerInputParser<T>::IntegerInputParser(double prescale, bool is_complex) : InputParser(prescale, is_complex)
{
}

template <class T>
std::size_t
IntegerInputParser<T>::GetDataTypeSize() const
{
    return sizeof(T) * (this->is_complex_ ? 2 : 1);
}

template <class T>
std::size_t
IntegerInputParser<T>::ParseBlock(const std::vector<char> &block)
{
    /* this function assumes well structured blocks */
    std::size_t item_size = (this->is_complex_ ? 2 : 1) * sizeof(T);
    if (block.size() % item_size != 0) {
        throw std::runtime_error("block size must be a multiple of sizeof(datatype)");
    }

    std::size_t count = block.size() / item_size;
    const T *start = reinterpret_cast<const T *>(block.data());

    /* parse one value at a time into complex target */
    for (std::size_t i = 0; i < count; i ++) {
        Complex value;
        if (this->is_complex_) {
            value = Complex(start[i * 2], start[i * 2 + 1]);
        } else {
            value = Complex(start[i], 0.0f);
        }

        /* normalize to domain limit */
        value /= (double)std::numeric_limits<T>::max();
        value *= this->prescale_factor_;
        this->values_.emplace_back(value);
    }

    return count;
}

template <class T>
FloatInputParser<T>::FloatInputParser(double prescale, bool is_complex) : InputParser(prescale, is_complex)
{
}

template <class T>
std::size_t
FloatInputParser<T>::GetDataTypeSize() const
{
    return sizeof(T) * (this->is_complex_ ? 2 : 1);
}

template <class T>
std::size_t
FloatInputParser<T>::ParseBlock(const std::vector<char> &block)
{
    /* this function assumes well structured blocks */
    std::size_t item_size = (this->is_complex_ ? 2 : 1) * sizeof(T);
    if (block.size() % item_size != 0) {
        throw std::runtime_error("block size must be a multiple of sizeof(datatype)");
    }

    std::size_t count = block.size() / item_size;
    const T *start = reinterpret_cast<const T *>(block.data());

    /* parse one value at a time into complex target */
    for (std::size_t i = 0; i < count; i ++) {
        Complex value;
        /* remove NaNs */
        if (this->is_complex_) {
            value = Complex(std::isnan(start[i * 2]) ? 0.0f : start[i * 2],
                            std::isnan(start[i * 2 + 1]) ? 0.0f : start[i * 2 + 1]);
        } else {
            value = Complex(std::isnan(start[i]) ? 0.0f : start[i], 0.0f);
        }

        /* prescale */
        value *= this->prescale_factor_;
        this->values_.emplace_back(value);
    }

    return count;
}

template class IntegerInputParser<int8_t>;
template class IntegerInputParser<int16_t>;
template class IntegerInputParser<int32_t>;
template class IntegerInputParser<int64_t>;

template class IntegerInputParser<uint8_t>;
template class IntegerInputParser<uint16_t>;
template class IntegerInputParser<uint32_t>;
template class IntegerInputParser<uint64_t>;

template class FloatInputParser<float>;
template class FloatInputParser<double>;