moved crc and xorshift from DSP to CODE repository

This commit is contained in:
Ahmet Inan 2018-09-20 08:45:26 +02:00
commit ef6871f94b
4 changed files with 0 additions and 263 deletions

View file

@ -38,22 +38,6 @@ Read and write [WAV](https://en.wikipedia.org/wiki/WAV) files
Algorithm for computing uniform and [natural cubic splines](https://en.wikipedia.org/wiki/Spline_(mathematics)#Algorithm_for_computing_natural_cubic_splines)
Very useful for data interpolation.
### [crc.hh](crc.hh)
A [Cyclic redundancy check](https://en.wikipedia.org/wiki/Cyclic_redundancy_check) may not really be DSP-related, but it is needed over and over again when you do DSP that it fits just perfectly here.
For example, if we need to integrate CRC32 checking for a few bytes, like in the following:
```
# echo -n 'Hello World!' | rhash -C -
(stdin) 1C291CA3
```
We can add it to our project as simple as that:
```
DSP::CRC<uint32_t> crc(0xEDB88320, 0xFFFFFFFF);
for (uint8_t c: std::string("Hello World!")) crc(c);
assert(~crc() == 0x1C291CA3);
```
### [regression.hh](regression.hh)
Implemented [Simple linear regression](https://en.wikipedia.org/wiki/Simple_linear_regression) for [Regression analysis](https://en.wikipedia.org/wiki/Regression_analysis) of data.
@ -70,12 +54,6 @@ Mixed-radix [decimation-in-time](https://en.wikipedia.org/wiki/Cooley%E2%80%93Tu
Some everyday helpers, like the [signum function](https://en.wikipedia.org/wiki/Sign_function) or the [lerp function](https://en.wikipedia.org/wiki/Linear_interpolation).
### [xorshift.hh](xorshift.hh)
Sometimes we need a sequence of ["random enough"](https://en.wikipedia.org/wiki/Diehard_tests) numbers but don't want to store them in an array to get a repeatable sequence.
Here a [Pseudorandom number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator) can help by prodiving a deterministic and thus repeatable sequence of numbers.
[George Marsaglia](https://en.wikipedia.org/wiki/George_Marsaglia) discovered a class of simple and fast pseudorandom number generators, which he called [Xorshift](https://en.wikipedia.org/wiki/Xorshift).
### [resampler.hh](resampler.hh)
When working with [Analog-to-digital](https://en.wikipedia.org/wiki/Analog-to-digital_converter) and [Digital-to-analog](https://en.wikipedia.org/wiki/Digital-to-analog_converter) converters, we often face the ugly truth, that we can't always have a precise [Sampling](https://en.wikipedia.org/wiki/Sampling_(signal_processing)) rate.

88
crc.hh
View file

@ -1,88 +0,0 @@
/*
Cyclic redundancy check
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#ifndef CRC_HH
#define CRC_HH
namespace DSP {
template <typename TYPE>
class CRC
{
TYPE lut[256];
TYPE poly;
TYPE crc;
TYPE update(TYPE prev, bool data)
{
TYPE tmp = prev ^ data;
return (prev >> 1) ^ ((tmp & 1) * poly);
}
public:
CRC(TYPE poly, TYPE crc = 0) : poly(poly), crc(crc)
{
for (int j = 0; j < 256; ++j) {
TYPE tmp = j;
for (int i = 8; i; --i)
tmp = update(tmp, 0);
lut[j] = tmp;
}
}
void reset(TYPE v = 0)
{
crc = v;
}
TYPE operator()()
{
return crc;
}
TYPE operator()(bool data)
{
return crc = update(crc, data);
}
TYPE operator()(uint8_t data)
{
TYPE tmp = crc ^ data;
return crc = (crc >> 8) ^ lut[tmp & 255];
}
TYPE operator()(uint16_t data)
{
(*this)(uint8_t(data & 255));
(*this)(uint8_t((data >> 8) & 255));
return crc;
}
TYPE operator()(uint32_t data)
{
(*this)(uint8_t(data & 255));
(*this)(uint8_t((data >> 8) & 255));
(*this)(uint8_t((data >> 16) & 255));
(*this)(uint8_t((data >> 24) & 255));
return crc;
}
TYPE operator()(uint64_t data)
{
(*this)(uint8_t(data & 255));
(*this)(uint8_t((data >> 8) & 255));
(*this)(uint8_t((data >> 16) & 255));
(*this)(uint8_t((data >> 24) & 255));
(*this)(uint8_t((data >> 32) & 255));
(*this)(uint8_t((data >> 40) & 255));
(*this)(uint8_t((data >> 48) & 255));
(*this)(uint8_t((data >> 56) & 255));
return crc;
}
};
template<>
uint8_t CRC<uint8_t>::operator()(uint8_t data)
{
return crc = lut[crc ^ data];
}
}
#endif

View file

@ -1,37 +0,0 @@
/*
Test for the Cyclic redundancy check
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#include <cassert>
#include <iostream>
#include <bitset>
#include "crc.hh"
int main()
{
if (1) {
DSP::CRC<uint32_t> crc(0xEDB88320, 0xFFFFFFFF);
for (uint8_t c: std::string("Hello World!")) crc(c);
assert(~crc() == 0x1C291CA3);
}
if (1) {
DSP::CRC<uint16_t> crc(0xA8F4);
for (uint8_t c: std::string("Hello World!")) crc(c);
assert(!crc(uint16_t(0x8FEF)));
crc.reset(0x9B38);
std::bitset<32> hw("00000010001001110111110100100100");
for (size_t i = 0; i < hw.size(); ++i) crc(hw[i]);
//std::cerr << "0x" << std::hex << crc() << std::endl;
assert(crc() == 0x915D);
}
if (1) {
DSP::CRC<uint8_t> crc(0x8C);
for (uint8_t c: std::string("Hello World!")) crc(c);
assert(!crc(uint8_t(0x9E)));
}
std::cerr << "Cyclic redundancy check test passed!" << std::endl;
return 0;
}

View file

@ -1,116 +0,0 @@
/*
Class of pseudorandom number generators, discovered by George Marsaglia
Copyright 2018 Ahmet Inan <inan@aicodix.de>
*/
#ifndef XORSHIFT_HH
#define XORSHIFT_HH
namespace DSP {
class Xorshift32
{
static const uint32_t Y = 2463534242;
uint32_t y_;
public:
Xorshift32(uint32_t y = Y) : y_(y) {}
void reset(uint32_t y = Y)
{
y_ = y;
}
uint32_t operator()()
{
y_ ^= y_ << 13;
y_ ^= y_ >> 17;
y_ ^= y_ << 5;
return y_;
}
};
class Xorshift64
{
static const uint64_t X = 88172645463325252;
uint64_t x_;
public:
Xorshift64(uint64_t x = X) : x_(x) {}
void reset(uint64_t x = X)
{
x_ = x;
}
uint64_t operator()()
{
x_ ^= x_ << 13;
x_ ^= x_ >> 7;
x_ ^= x_ << 17;
return x_;
}
};
class Xorwow
{
static const uint32_t X = 123456789;
static const uint32_t Y = 362436069;
static const uint32_t Z = 521288629;
static const uint32_t W = 88675123;
static const uint32_t V = 5783321;
static const uint32_t D = 6615241;
uint32_t x_, y_, z_, w_, v_, d_;
public:
Xorwow(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W,
uint32_t v = V, uint32_t d = D) :
x_(x), y_(y), z_(z), w_(w), v_(v), d_(d) {}
void reset(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W,
uint32_t v = V, uint32_t d = D)
{
x_ = x;
y_ = y;
z_ = z;
w_ = w;
v_ = v;
d_ = d;
}
uint32_t operator()()
{
uint32_t t = x_ ^ (x_ >> 2);
x_ = y_; y_ = z_; z_ = w_; w_ = v_;
v_ = (v_ ^ (v_ << 4)) ^ (t ^ (t << 1));
d_ += 362437;
return d_ + v_;
}
};
class Xorshift128
{
static const uint32_t X = 123456789;
static const uint32_t Y = 362436069;
static const uint32_t Z = 521288629;
static const uint32_t W = 88675123;
uint32_t x_, y_, z_, w_;
public:
Xorshift128(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W) :
x_(x), y_(y), z_(z), w_(w) {}
void reset(uint32_t x = X, uint32_t y = Y,
uint32_t z = Z, uint32_t w = W)
{
x_ = x;
y_ = y;
z_ = z;
w_ = w;
}
uint32_t operator()()
{
uint32_t t = (x_ ^ (x_ << 11));
x_ = y_; y_ = z_; z_ = w_;
w_ = (w_ ^ (w_ >> 19)) ^ (t ^ (t >> 8));
return w_;
}
};
}
#endif