Inverse Discrete Fourier Transform with Real to Real functions - signal-processing

I have a DFT (real_to_complex) output (5x8) that I want to inverse transform using r2r (real to real) functions, instead of iDFT, because the coefficients are all favorably real:
[0.000000] [0.000000] [0.000000] [0.000000] [0.000000]
[0.000000] [-0.000039] [-5.228421] [1.999998] [0.000000]
[0.000000] [10.456858] [-0.000000] [-0.329559] [0.000000]
[0.000000] [-6.000002] [0.494339] [-0.000000] [0.000000]
[0.000000] [0.000000] [0.000000] [0.000000] [0.000000]
[0.000000] [12.000002] [-2.471695] [2.402020] [0.000000]
[0.000000] [-31.370562] [4.000000] [-1.647797] [0.000000]
[0.000000] [81.597977] [-15.685284] [4.000000] [0.000000]
The problem is with symmetry, the X-axis is fine; following the pattern of odd-symmetric DST-I 0,a,b,c,0,-c,-b,-a (we don't see the -c,-b,-a because this is "real_to_complex" output ). However, the Y-axis is not following DST pattern or the DCT-I pattern a,b,c,d,e,d,c,b. I don't see it being symmetric however, because the coefficients are real and the output is real, we should be able to calculate this with only real values.
The iDFT 8x8 output is the following:
[1.694307] [1.331561] [0.590346] [-1.331561] [-2.874999] [-1.331561] [0.590346] [1.331561]
[1.331561] [1.625000] [1.256080] [-0.281785] [-2.168437] [-1.968213] [-0.419204] [0.624999]
[0.590346] [1.580797] [2.125001] [1.212098] [-0.840346] [-2.048974] [-1.875001] [-0.743921]
[-1.331561] [1.031788] [2.951028] [4.125000] [2.168437] [-1.875002] [-3.787904] [-3.281786]
[-2.874999] [-2.168437] [-0.840346] [2.168437] [4.555690] [2.168437] [-0.840346] [-2.168437]
[-1.331561] [-3.281786] [-3.787904] [-1.875002] [2.168437] [4.125000] [2.951028] [1.031788]
[0.590346] [-0.743921] [-1.875001] [-2.048974] [-0.840346] [1.212098] [2.125001] [1.580797]
[1.331561] [0.624999] [-0.419204] [-1.968213] [-2.168437] [-0.281785] [1.256080] [1.625000]

The complex_to_real DFT assumes complex conjugate symmetry for the 8*5 matrix.
When we expand this matrix into 8-point DFTs of Columns and Rows, the columns do not meet the odd symmetry criteria [0,a,b,c,0,-c,-b,-a] and as such seek contribution from the cosine basis, producing a complex value.
However when we examine the columns in frequency space, there is somewhat an odd symmetry amongst complex values.
In Matlab we define the matrix R1 as follows:
0 0 0 0 0 0 0 0
0 -0.000039 -5.228421 1.999998 0 -1.999998 5.228421 0.000039
0 10.456858 -0 -0.329559 0 0.329559 0 -10.456858
0 -6.000002 0.494339 -0 0 0 -0.494339 6.000002
0 0 0 0 0 0 0 0
0 12.000002 -2.471695 2.40202 0 -2.40202 2.471695 -12.000002
0 -31.370562 4 -1.647797 0 1.647797 -4 31.370562
0 81.597977 -15.685284 4 0 -4 15.685284 -81.597977
Then the colFFR2=ifft(R1 ) produces the output;
0.000000 + 0.000000i 8.335529 + 0.000000i -2.361383 + 0.000000i 0.8030828 + 0.000000i 0.000000 + 0.000000i -0.8030828 + 0.000000i 2.361383 + 0.000000i -8.335529 + 0.000000i
0.000000 + 0.000000i 6.681977 - 3.574877i -1.673753 + 0.6864277i 0.3180193 - 0.2243077i 0.000000 + 0.000000i -0.3180193 + 0.2243077i 1.673753 - 0.6864277i -6.681977 + 3.574877i
0.000000 + 0.000000i 2.614213 - 7.949751i -0.5000000 + 0.9363536i 0.2471695 + 0.05025225i 0.000000 + 0.000000i -0.2471695 - 0.05025225i 0.5000000 - 0.9363536i -2.614213 + 7.949751i
0.000000 + 0.000000i -6.681977 - 14.03173i 1.673753 + 1.686428i -0.3180193 - 0.5538672i 0.000000 + 0.000000i 0.3180193 + 0.5538672i -1.673753 - 1.686428i 6.681977 + 14.03173i
0.000000 + 0.000000i -13.56396 + 0.000000i 3.361383 + 0.000000i -1.297422 + 0.000000i 0.000000 + 0.000000i 1.297422 + 0.000000i -3.361383 + 0.000000i 13.56396 + 0.000000i
0.000000 + 0.000000i -6.681977 + 14.03173i 1.673753 - 1.686428i -0.3180193 + 0.5538672i 0.000000 + 0.000000i 0.3180193 - 0.5538672i -1.673753 + 1.686428i 6.681977 - 14.03173i
0.000000 + 0.000000i 2.614213 + 7.949751i -0.5000000 - 0.9363536i 0.2471695 - 0.05025225i 0.000000 + 0.000000i -0.2471695 + 0.05025225i 0.5000000 + 0.9363536i -2.614213 - 7.949751i
0.000000 + 0.000000i 6.681977 + 3.574877i -1.673753 - 0.6864277i 0.3180193 + 0.2243077i 0.000000 + 0.000000i -0.3180193 - 0.2243077i 1.673753 + 0.6864277i -6.681977 - 3.574877i
Finally ifft(colFFR2.', 'symmetric') provides the matching iDFT.

Related

xarray transpose: TypeError: unhashable type: 'list'

I am trying to rearrange the dimensions of the following dataset
X
xarray.Dataset
Dimensions:
lon: 720lat: 360sector: 8time: 240
Coordinates:
lon
(lon)
float64
-179.8 -179.2 ... 179.2 179.8
lat
(lat)
float64
-89.75 -89.25 ... 89.25 89.75
sector
(sector)
int32
0 1 2 3 4 5 6 7
time
(time)
object
2000-01-16 00:00:00 ... 2019-12-...
Data variables:
CO_em_anthro
(time, sector, lat, lon)
float32
0.0 0.0 0.0 0.0 ... 0.0 0.0 0.0 0.0
Attributes: (33)
This used to work
X.transpose(['lat','lon','sector','time'])
but in version 0.20.1, I am getting the following error
File /nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py:879, in drop_missing_dims(supplied_dims, dims, missing_dims)
868 """Depending on the setting of missing_dims, drop any dimensions from supplied_dims that
869 are not present in dims.
870
(...)
875 missing_dims : {"raise", "warn", "ignore"}
876 """
878 if missing_dims == "raise":
--> 879 supplied_dims_set = {val for val in supplied_dims if val is not ...}
880 invalid = supplied_dims_set - set(dims)
881 if invalid:
File /nbhome/f1p/miniconda3/envs/f1p_gfdl/lib/python3.9/site-packages/xarray/core/utils.py:879, in <setcomp>(.0)
868 """Depending on the setting of missing_dims, drop any dimensions from supplied_dims that
869 are not present in dims.
870
(...)
875 missing_dims : {"raise", "warn", "ignore"}
876 """
878 if missing_dims == "raise":
--> 879 supplied_dims_set = {val for val in supplied_dims if val is not ...}
880 invalid = supplied_dims_set - set(dims)
881 if invalid:
TypeError: unhashable type: 'list'
calling transpose does work without the dimension name.
I am not sure how to fix this issue. Thanks
Xarray’s transpose accepts the target dimensions as multiple arguments, not a list of dimensions.
See the *args in the transpose docs.
You need to change your code to:
X.transpose('lat','lon','sector','time')

Define a function : Fibonacci Sequence

Define a function to implement Fibonacci Sequence: 1, 1, 2, 3, 5, 8, 13, 21, 34. Please use the function output first 20 figures of Fibonacci Sequence.
Here is a python implementation
def fib(n):
a, b = 0, 1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()
fib(5000)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181
A recursive implementation
memo = [-1] * 21
memo[0] = 0
memo[1] = 1
print(memo[0], end=' ')
print(memo[1], end=' ')
def fibrec(n):
if(memo[n] == -1):
memo[n] = fibrec(n-2) + fibrec(n-1)
print(memo[n], end=' ')
return memo[n]
fibrec(20)
Output
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765

Extract text from wrk output

I'm running a load test with wrk2 as a job on Jenkins. I'd like to send the results of the load test to Graylog but I only want to store the Requests/Sec and average latency.
Here's what the output looks like:
Running 30s test # https://example.com
1 threads and 100 connections
Thread calibration: mean lat.: 8338.285ms, rate sampling interval: 19202ms
Thread Stats Avg Stdev Max +/- Stdev
Latency 16.20s 6.17s 29.64s 65.74%
Req/Sec 5.00 0.00 5.00 100.00%
Latency Distribution (HdrHistogram - Recorded Latency)
50.000% 15.72s
75.000% 20.81s
90.000% 24.58s
99.000% 29.13s
99.900% 29.66s
99.990% 29.66s
99.999% 29.66s
100.000% 29.66s
Detailed Percentile spectrum:
Value Percentile TotalCount 1/(1-Percentile)
4497.407 0.000000 1 1.00
7561.215 0.100000 11 1.11
11100.159 0.200000 22 1.25
12582.911 0.300000 33 1.43
14565.375 0.400000 44 1.67
15720.447 0.500000 54 2.00
16416.767 0.550000 60 2.22
17301.503 0.600000 65 2.50
18464.767 0.650000 71 2.86
19185.663 0.700000 76 3.33
20807.679 0.750000 81 4.00
21479.423 0.775000 84 4.44
22347.775 0.800000 87 5.00
22527.999 0.825000 90 5.71
23216.127 0.850000 93 6.67
23478.271 0.875000 95 8.00
23805.951 0.887500 96 8.89
24723.455 0.900000 98 10.00
25067.519 0.912500 99 11.43
25395.199 0.925000 101 13.33
26525.695 0.937500 102 16.00
26525.695 0.943750 102 17.78
26705.919 0.950000 103 20.00
28065.791 0.956250 104 22.86
28065.791 0.962500 104 26.67
28377.087 0.968750 105 32.00
28377.087 0.971875 105 35.56
28475.391 0.975000 106 40.00
28475.391 0.978125 106 45.71
28475.391 0.981250 106 53.33
29130.751 0.984375 107 64.00
29130.751 0.985938 107 71.11
29130.751 0.987500 107 80.00
29130.751 0.989062 107 91.43
29130.751 0.990625 107 106.67
29655.039 0.992188 108 128.00
29655.039 1.000000 108 inf
#[Mean = 16199.756, StdDeviation = 6170.105]
#[Max = 29638.656, Total count = 108]
#[Buckets = 27, SubBuckets = 2048]
----------------------------------------------------------
130 requests in 30.02s, 13.44MB read
Socket errors: connect 0, read 0, write 0, timeout 1192
Requests/sec: 4.33
Transfer/sec: 458.47KB
Does anyone know how I could go about extracting Requests/sec (at the bottom) and the latency average to send as JSON parameters?
The expected output would be: "latency": 16.2, "requests_per_second": 4.33
You didn't provide the expected output so your question isn't clear but is this what you want?
$ awk 'BEGIN{a["Latency"]; a["Requests/sec:"]} ($1 in a) && ($2 ~ /[0-9]/){print $1, $2}' file
Latency 16.20s
Requests/sec: 4.33
Updated based on you adding expected output to your question:
$ awk '
BEGIN { map["Latency"]="latency"; map["Requests/sec:"]="requests_per_second" }
($1 in map) && ($2 ~ /[0-9]/) { printf "%s\"%s\": %s", ofs, map[$1], $2+0; ofs=", " }
END { print "" }
' file
"latency": 16.2, "requests_per_second": 4.33

LSTM predicting time series yields odd results

I'm trying to predict time series data for the next few days looking at past few days, using Keras. My label data is target values for multiple future days, regression model has multiple output neurons (the "direct approach" for time series).
Here is test data with predictions for 10 days, using 60 days history.
10 days prediction for test data
As you can see, future values for all days are about the same. I've spent quite some time on it, and must admit that I'm probably missing something with respect to LSTM...
Here is training data with prediction:
10 days prediction for training data
In order to confirm that I'm preparing data properly, I've created a "tracking data set" which I used to visualize data transformations. Here it is...
Data set:
Open,High,Low,Close,Volume,OpenInt
111,112,113,114,115,0
121,122,123,124,125,0
131,132,133,134,135,0
141,142,143,144,145,0
151,152,153,154,155,0
161,162,163,164,165,0
171,172,173,174,175,0
181,182,183,184,185,0
191,192,193,194,195,0
201,202,203,204,205,0
211,212,213,214,215,0
221,222,223,224,225,0
231,232,233,234,235,0
241,242,243,244,245,0
251,252,253,254,255,0
261,262,263,264,265,0
271,272,273,274,275,0
281,282,283,284,285,0
291,292,293,294,295,0
Training set using 2 days history, predicting 3 days future values (I used different values of history days and future days, and it all makes sense to me), without feature scaling in order to visualize data transformations:
X train (6, 2, 5)
[[[111 112 113 114 115]
[121 122 123 124 125]]
[[121 122 123 124 125]
[131 132 133 134 135]]
[[131 132 133 134 135]
[141 142 143 144 145]]
[[141 142 143 144 145]
[151 152 153 154 155]]
[[151 152 153 154 155]
[161 162 163 164 165]]
[[161 162 163 164 165]
[171 172 173 174 175]]]
Y train (6, 3)
[[131 141 151]
[141 151 161]
[151 161 171]
[161 171 181]
[171 181 191]
[181 191 201]]
Test set
X test (6, 2, 5)
[[[201 202 203 204 205]
[211 212 213 214 215]]
[[211 212 213 214 215]
[221 222 223 224 225]]
[[221 222 223 224 225]
[231 232 233 234 235]]
[[231 232 233 234 235]
[241 242 243 244 245]]
[[241 242 243 244 245]
[251 252 253 254 255]]
[[251 252 253 254 255]
[261 262 263 264 265]]]
Y test (6, 3)
[[221 231 241]
[231 241 251]
[241 251 261]
[251 261 271]
[261 271 281]
[271 281 291]]
Model:
def CreateRegressor(self,
optimizer='adam',
activation='tanh', # RNN activation
init_mode='glorot_uniform',
hidden_neurons=50,
dropout_rate=0.0,
weight_constraint=0,
stateful=False,
# SGD parameters
learn_rate=0.01,
momentum=0):
kernel_constraint = maxnorm(weight_constraint) if weight_constraint > 0 else None
model = Sequential()
model.add(LSTM(units=hidden_neurons, activation=activation, kernel_initializer=init_mode, kernel_constraint=kernel_constraint,
return_sequences=True, input_shape=(self.X_train.shape[1], self.X_train.shape[2]), stateful=stateful))
model.add(Dropout(dropout_rate))
model.add(LSTM(units=hidden_neurons, activation=activation, kernel_initializer=init_mode, kernel_constraint=kernel_constraint,
return_sequences=True, stateful=stateful))
model.add(Dropout(dropout_rate))
model.add(LSTM(units=hidden_neurons, activation=activation, kernel_initializer=init_mode, kernel_constraint=kernel_constraint,
return_sequences=True, stateful=stateful))
model.add(Dropout(dropout_rate))
model.add(LSTM(units=hidden_neurons, activation=activation, kernel_initializer=init_mode, kernel_constraint=kernel_constraint,
return_sequences=False, stateful=stateful))
model.add(Dropout(dropout_rate))
model.add(Dense(units=self.y_train.shape[1]))
if (optimizer == 'SGD'):
optimizer = SGD(lr=learn_rate, momentum=momentum)
model.compile(optimizer=optimizer, loss='mean_squared_error')
return model
...which I create with these params:
self.CreateRegressor(optimizer = 'adam', hidden_neurons = 100)
... and then fit like this:
self.regressor.fit(self.X_train, self.y_train, epochs=100, batch_size=32)
... and predict:
y_pred = self.regressor.predict(X_test)
... or
y_pred_train = self.regressor.predict(X_train)
What am I missing?

Try to simulate a neural network in MATLAB by myself

I tried to create a neural network to estimate y = x ^ 2. So I created a fitting neural network and gave it some samples for input and output. I tried to build this network in C++. But the result is different than I expected.
With the following inputs:
0 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 -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
and the following outputs:
0 1 4 9 16 25 36 49 64 81 100 121 144 169 196 225 256 289 324 361 400
441 484 529 576 625 676 729 784 841 900 961 1024 1089 1156 1225 1296
1369 1444 1521 1600 1681 1764 1849 1936 2025 2116 2209 2304 2401 2500
2601 2704 2809 2916 3025 3136 3249 3364 3481 3600 3721 3844 3969 4096
4225 4356 4489 4624 4761 4900 5041 1 4 9 16 25 36 49 64 81 100 121 144
169 196 225 256 289 324 361 400 441 484 529 576 625 676 729 784 841
900 961 1024 1089 1156 1225 1296 1369 1444 1521 1600 1681 1764 1849
1936 2025 2116 2209 2304 2401 2500 2601 2704 2809 2916 3025 3136 3249
3364 3481 3600 3721 3844 3969 4096 4225 4356 4489 4624 4761 4900 5041
I used fitting tool network. with matrix rows. Training is 70%, validation is 15% and testing is 15% as well. The number of hidden neurons is two. Then in command lines I wrote this:
purelin(net.LW{2}*tansig(net.IW{1}*inputTest+net.b{1})+net.b{2})
Other information :
My net.b[1] is: -1.16610230053776 1.16667147712026
My net.b[2] is: 51.3266249426358
And net.IW(1) is: 0.344272596370387 0.344111217766824
net.LW(2) is: 31.7635369693519 -31.8082184881063
When my inputTest is 3, the result of this command is 16, while it should be about 9. Have I made an error somewhere?
I found the Stack Overflow post Neural network in MATLAB that contains a problem like my problem, but there is a little difference, and the differences is in that problem the ranges of input and output are same, but in my problem is no. That solution says I need to scale out the results, but how can I scale out my result?
You are right about scaling. As was mentioned in the linked answer, the neural network by default scales the input and output to the range [-1,1]. This can be seen in the network processing functions configuration:
>> net = fitnet(2);
>> net.inputs{1}.processFcns
ans =
'removeconstantrows' 'mapminmax'
>> net.outputs{2}.processFcns
ans =
'removeconstantrows' 'mapminmax'
The second preprocessing function applied to both input/output is mapminmax with the following parameters:
>> net.inputs{1}.processParams{2}
ans =
ymin: -1
ymax: 1
>> net.outputs{2}.processParams{2}
ans =
ymin: -1
ymax: 1
to map both into the range [-1,1] (prior to training).
This means that the trained network expects input values in this range, and outputs values also in the same range. If you want to manually feed input to the network, and compute the output yourself, you have to scale the data at input, and reverse the mapping at the output.
One last thing to remember is that each time you train the ANN, you will get different weights. If you want reproducible results, you need to fix the state of the random number generator (initialize it with the same seed each time). Read the documentation on functions like rng and RandStream.
You also have to pay attention that if you are dividing the data into training/testing/validation sets, you must use the same split each time (probably also affected by the randomness aspect I mentioned).
Here is an example to illustrate the idea (adapted from another post of mine):
%%# data
x = linspace(-71,71,200); %# 1D input
y_model = x.^2; %# model
y = y_model + 10*randn(size(x)).*x; %# add some noise
%%# create ANN, train, simulate
net = fitnet(2); %# one hidden layer with 2 nodes
net.divideFcn = 'dividerand';
net.trainParam.epochs = 50;
net = train(net,x,y);
y_hat = net(x);
%%# plot
plot(x, y, 'b.'), hold on
plot(x, x.^2, 'Color','g', 'LineWidth',2)
plot(x, y_hat, 'Color','r', 'LineWidth',2)
legend({'data (noisy)','model (x^2)','fitted'})
hold off, grid on
%%# manually simulate network
%# map input to [-1,1] range
[~,inMap] = mapminmax(x, -1, 1);
in = mapminmax('apply', x, inMap);
%# propagate values to get output (scaled to [-1,1])
hid = tansig( bsxfun(#plus, net.IW{1}*in, net.b{1}) ); %# hidden layer
outLayerOut = purelin( net.LW{2}*hid + net.b{2} ); %# output layer
%# reverse mapping from [-1,1] to original data scale
[~,outMap] = mapminmax(y, -1, 1);
out = mapminmax('reverse', outLayerOut, outMap);
%# compare against MATLAB output
max( abs(out - y_hat) ) %# this should be zero (or in the order of `eps`)
I opted to use the mapminmax function, but you could have done that manually as well. The formula is a pretty simply linear mapping:
y = (ymax-ymin)*(x-xmin)/(xmax-xmin) + ymin;

Resources