How to obtain new data from the given starting point? - matlab

I am new in Neural network and matlab. My problem -> I have some XYgraphs (X-data, Y-Time). All graphs have same time, but different X values. Also I have a starting point Z. I want to get the actual graph which start from Z, based on above said XY graphs. I tried it by using "nntool" which was available in matlab. I tried few algorithms like TRAINBR, TRAINLM, TRAINB etc. But the output of the trained network never starts from Z. I tried arranging my data, changed input ranges, tried with higher number of layers, epochs, neurons etc. Nothing worked. Please tell me how to solve this problem. No need to use nntool itself.You can suggest any better options... Please help me... A example picture is here...

From what I can infer, you are trying to interpolate. Naively one can do it by shifting the mean of the data to Z. I don't have MATLAB, but it shouldn't be difficult to read the Python code.
import matplotlib.pyplot as plt
import numpy as np
Z = 250
# Creating some fake data
y = np.zeros((1000,3))
y[:,0] = np.arange(1000)-500
y[:,1] = np.arange(1000)
y[:,2] = np.arange(1000)+500
x = np.arange(1000)
# Plotting fake data
plt.plot(x,y)
#Take mean along Y axis
ymean = np.mean(y,axis=1)
# Shift the mean to the desired Z after shifting it to origin
Zdash = ymean + (Z - ymean[0])
plt.plot(x,y)
plt.plot(x,Zdash)

Related

Matlab findpeaks unable to detect peak

I have a 1d signal with a very clear peak, which I wish to characterise. For this, Matlab recommends the findpeak() function. I used it, but it could not detect any peaks.
Can anyone tell me why the peak detection fails, and how I could correct this? Is it because of the slight asymmetry, or because of its location close to the origin?
Here is the signal:
-0.749429547824983,-1.48893649456578,-3.69142404184161,-5.90518397007934,-8.94050442864937,-13.9397113416771,-18.6485000060921,-26.171050819591,-33.8321611238473,-42.6887187100784,-52.7848046259627,-64.0885716722416,-76.4819754427361,-89.7534943093904,-103.596636684011,-117.616848525362,-137.728485864984,-149.968644032202,-165.760526765682,-173.438446162355,-177.794367330021,-180.665504657482,-181.587837865151,-182.05669036563,-181.953081445756,-181.224964655595,-179.466956009378,-174.935326783301,-166.108729320377,-154.8188727578,-138.989450390713,-124.745660884327,-110.75406648731,-97.5101283485409,-85.3020579114654,-75.9579975036876,-67.4601854156705,-61.2940646311251,-55.6332658221936,-50.4524017554082,-45.7226429946525,-41.4134661940384,-37.4939178270878,-33.9335009233627,-30.7027744357005,-27.7737375478043,-25.120055867709,-22.7171735026633,-20.5423444708846,-18.1299037240041,-16.0034032566806,-13.4349709052746,-11.2814814601965,-9.22383728236694,-7.54259823223305,-6.16891326304915,-5.04643005083767,-4.12908680572436,-3.3792629187093,-2.76624567764036,-2.26496715909135,-1.85496794618472,-1.51954915870186,-1.24507971658957,-1.02043099016049,-0.836515683557349,-0.685911850401105,-0.562556362193813,-0.461494999843215,-0.378678691801432,-0.31079735465677,-0.255144373245673,-0.209506048358245,-0.172071392676599,-0.141358513015601,-0.116154515064569,-0.0954664351129079,-0.0784811657953865,-0.0645327193959676,-0.053075478741892,-0.0436623352584338,-0.035926816959731,-0.0295684746597058,-0.0243409295220307,-0.0200420949390602,-0.0165061752797916,-0.0135971170520871,-0.0112032475556222,-0.00923288465794644,-0.00761074093974044,-0.00627497778144994,-0.00517479134910001,-0.00426843397916662,-0.00352159205421904,-0.00290605583052415,-0.00239862841900826,-0.00198023071526613,-0.00163516691682862,-0.00135052167802519,-0.00111566519662662,-0.000881935744953948,-0.000697915251838727,-0.000524637598946218,-0.000394567839006708,-0.000296885161988861,-0.00020976002143167,-0.000148545955794115,-9.72008185405489e-05,-6.36690385791491e-05,-4.17477119966825e-05,-2.74018483759325e-05,-1.8003902287259e-05,-1.18410413188178e-05,-7.7955452552938e-06,-5.13728164003599e-06,-3.38881214609516e-06,-2.2376175188872e-06,-1.47892273125427e-06,-9.78415790827604e-07,-6.47913246914071e-07,-4.29460109665109e-07,-2.84930223745026e-07,-1.89217408323062e-07,-1.25773079886823e-07,-8.36787965589242e-08,-5.57238587861215e-08,-3.71416697587417e-08,-2.47783039314225e-08,-1.6544922828963e-08,-1.10567854116822e-08,-7.39503946153151e-09,-4.94938072790637e-09,-3.31402364108024e-09,-2.2188483621877e-09,-1.48377653147047e-09,-9.88495439963603e-10,-6.52325826840569e-10,-4.20789022022039e-10,-2.56587161450693e-10,-1.50808306057169e-10,-6.34502662515917e-11,-3.11362883724889e-11
and here is the x-data:
0.7,0.700231481481481,0.700462962962963,0.700925925925926,0.701388888888889,0.701851851851852,0.702777777777778,0.703472222222222,0.704166666666667,0.704861111111111,0.705555555555555,0.70625,0.706944444444444,0.707638888888889,0.708333333333333,0.709027777777778,0.709722222222222,0.711111111111111,0.711805555555555,0.7125,0.713194444444444,0.713541666666667,0.713888888888889,0.71412037037037,0.714351851851852,0.714814814814815,0.715277777777778,0.715740740740741,0.716666666666667,0.718055555555556,0.719097222222222,0.720138888888889,0.721180555555555,0.722222222222222,0.723263888888889,0.724305555555556,0.725,0.725694444444444,0.726388888888889,0.727083333333333,0.727777777777778,0.728472222222222,0.729166666666667,0.729861111111111,0.730555555555556,0.73125,0.731944444444444,0.732638888888889,0.733333333333333,0.734027777777778,0.735069444444444,0.736111111111111,0.7375,0.738888888888889,0.740277777777778,0.741666666666667,0.743055555555555,0.744444444444444,0.745833333333333,0.747222222222222,0.748611111111111,0.75,0.751388888888889,0.752777777777778,0.754166666666667,0.755555555555556,0.756944444444444,0.758333333333333,0.759722222222222,0.761111111111111,0.7625,0.763888888888889,0.765277777777778,0.766666666666667,0.768055555555555,0.769444444444444,0.770833333333333,0.772222222222222,0.773611111111111,0.775,0.776388888888889,0.777777777777778,0.779166666666667,0.780555555555555,0.781944444444444,0.783333333333333,0.784722222222222,0.786111111111111,0.7875,0.788888888888889,0.790277777777778,0.791666666666667,0.793055555555555,0.794444444444444,0.795833333333333,0.797222222222222,0.798611111111111,0.8,0.801388888888889,0.802777777777778,0.804166666666667,0.80625,0.808333333333333,0.810416666666667,0.8125,0.814583333333333,0.817708333333333,0.820833333333333,0.823958333333333,0.827083333333333,0.830208333333333,0.833333333333333,0.836458333333333,0.839583333333333,0.842708333333333,0.845833333333333,0.848958333333333,0.852083333333333,0.855208333333333,0.858333333333333,0.861458333333333,0.864583333333333,0.867708333333333,0.870833333333333,0.873958333333333,0.877083333333333,0.880208333333333,0.883333333333333,0.886458333333333,0.889583333333333,0.892708333333333,0.895833333333333,0.898958333333333,0.902083333333333,0.905208333333333,0.908333333333333,0.911458333333333,0.914583333333333,0.917708333333333,0.920833333333333,0.922916666666667,0.925
I copy-pasted them from a comma-delimited .txt file.
If you look at the help you can see:
PKS = findpeaks(Y) finds local peaks in the data vector Y. A local peak
is defined as a data sample which is either larger than the two
neighboring samples or is equal to Inf.
So by default it is looking for a positive peak, you can supply the -ve of the signal and it will find it.
I was able to find the peak by fitting the posted data to a three-parameter Extreme Value peak equation, "y = a * exp(-exp(-((x-b)/c))-((x-b)/c)+1.0)", with parameter b as the peak at approximately x = 0.714. Fitted parameters of a = -1.7877846561352214E+02, b = 7.1375869130522429E-01, and c = 6.4029302224962413E-03 yielded RMSE = 2.64 and R-squared = 0.997.

matlab: cdfplot of relative error

The figure shown above is the plot of cumulative distribution function (cdf) plot for relative error (attached together the code used to generate the plot). The relative error is defined as abs(measured-predicted)/(measured). May I know the possible error/interpretation as the plot is supposed to be a smooth curve.
X = load('measured.txt');
Xhat = load('predicted.txt');
idx = find(X>0);
x = X(idx);
xhat = Xhat(idx);
relativeError = abs(x-xhat)./(x);
cdfplot(relativeError);
The input data file is a 4x4 matrix with zeros on the diagonal and some unmeasured entries (represent with 0). Appreciate for your kind help. Thanks!
The plot should be a discontinuous one because you are using discrete data. You are not plotting an analytic function which has an explicit (or implicit) function that maps, say, x to y. Instead, all you have is at most 16 points that relates x and y.
The CDF only "grows" when new samples are counted; otherwise its value remains steady, just because there isn't any satisfying sample that could increase the "frequency".
You can check the example in Mathworks' `cdfplot1 documentation to understand the concept of "empirical cdf". Again, only when you observe a sample can you increase the cdf.
If you really want to "get" a smooth curve, either 1) add more points so that the discontinuous line looks smoother, or 2) find any statistical model of whatever you are working on, and plot the analytic function instead.

Input signal for Simulink

I want to use the following signal (red) in Simulink as input.
All I have is this picture. Any advice on the simplest way to implement this signal?
Your question has two parts: bringing the data to work space of Matlab and feeding the data to Simulink.
For the first part I think the simplest thing is to put about 30 points on the figure and write their estimated (x,y) values in vectors X and Y. it should not be hard because the first part of it is periodic.
Then use plot(X,Y) to plot this vector in Matlab and update your estimated values till you are satisfied that your plot is similar to the figure.
For the second part you can create a structure where time is the same as your X axis and Y as the values:
input.time = X;
input.signals.values = Y;
where X and Y should have the same length.
you can find good examples of how to import signals from work space to Simulink at this page: https://www.mathworks.com/help/simulink/slref/fromworkspace.html

Outline your hand with splines (Matlab)

I'm trying to write a script so that one can put his hand on the screen, click a few points with ginput, and have matlab generate an outline of the persons hand using splines. However, I'm quite unsure how you can have splines connect points that result from your clicks, as they of course are described by some sort of parametrization. How can you use the spline command built into matlab when the points aren't supposed to be connected 'from left to right'?
The code I have so far is not much, it just makes a box and lets you click some points
FigHandle = figure('Position', [15,15, 1500, 1500]);
rectangle('Position',[0,0,40,40])
daspect([1,1,1])
[x,y] = ginput;
So I suppose my question is really what to do with x and y so that you can spline them in such a way that they are connected 'chronologically'. (And, in the end, connecting the last one to the first one)
look into function cscvn
curve = cscvn(points)
returns a parametric variational, or natural, cubic spline curve (in ppform) passing through the given sequence points(:j), j = 1:end.
An excellent example here:
http://www.mathworks.com/help/curvefit/examples/constructing-spline-curves-in-2d-and-3d.html
I've found an alternative for using the cscvn function.
Using a semi-arclength parametrisation, I can create the spline from the arrays x and y as follows:
diffx = diff(x);
diffy = diff(y);
t = zeros(1,length(x)-1);
for n = 1:length(x)-1
t(n+1) = t(n) + sqrt(diffx(n).^2+diffy(n).^2);
end
tj = linspace(t(1),t(end),300);
xj = interp1(t,x,tj,'spline');
yj = interp1(t,y,tj,'spline');
plot(x,y,'b.',xj,yj,'r-')
This creates pretty decent outlines.
What this does is use the fact that a curve in the plane can be approximated by connecting a finite number of points on the curve using line segments to create a polygonal path. Using this we can parametrize the points (x,y) in terms of t. As we only have a few points to create t from, we create more by adding linearly spaced points in between. Using the function interp1, we then find the intermediate values of x and y that correspond to these linearly spaced t, ti.
Here is an example of how to do it using linear interpolation: Interpolating trajectory from unsorted array of 2D points where order matters. This should get you to the same result as plot(x,y).
The idea in that post is to loop through each consecutive pair of points and interpolate between just those points. You might be able to adapt this to work with splines, you need to give it 4 points each time though which could cause problems since they could double back.
To connect the start and end though just do this before interpolating:
x(end+1) = x(1);
y(end+1) = y(1);

Interpolating irregularly spaced 3D matrix in matlab

I have a time series of temperature profiles that I want to interpolate, I want to ask how to do this if my data is irregularly spaced.
Here are the specifics of the matrix:
The temperature is 30x365
The time is 1x365
Depth is 30x1
Both time and depth are irregularly spaced. I want to ask how I can interpolate them into a regular grid?
I have looked at interp2 and TriScatteredInterp in Matlab, however the problem are the following:
interp2 works only if data is in a regular grid.
TriscatteredInterp works only if the vectors are column vectors. Although time and depth are both column vectors, temperature is not.
Thanks.
Function Interp2 does not require for a regularly spaced measurement grid at all, it only requires a monotonic one. That is, sampling positions stored in vectors depths and times must increase (or decrease) and that's all.
Assuming this is indeed is the situation* and that you want to interpolate at regular positions** stored in vectors rdepths and rtimes, you can do:
[JT, JD] = meshgrid(times, depths); %% The irregular measurement grid
[RT, RD] = meshgrid(rtimes, rdepths); %% The regular interpolation grid
TemperaturesOnRegularGrid = interp2(JT, JD, TemperaturesOnIrregularGrid, RT, RD);
* : If not, you can sort on rows and columns to come back to a monotonic grid.
**: In fact Interp2 has no restriction for output grid (it can be irregular or even non-monotonic).
I would use your data to fit to a spline or polynomial and then re-sample at regular intervals. I would highly recommend the polyfitn function. Actually, anything by this John D'Errico guy is incredible. Aside from that, I have used this function in the past when I had data on a irregularly spaced 3D problem and it worked reasonably well. If your data set has good support, which I suspect it does, this will be a piece of cake. Enjoy! Hope this helps!
Try the GridFit tool on MATLAB central by John D'Errico. To use it, pass in your 2 independent data vectors (time & temperature), the dependent data matrix (depth) along with the regularly spaced X & Y data points to use. By default the tool also does smoothing for overlapping (or nearly) data points. If this is not desired, you can override this (and other options) through a wide range of configuration options. Example code:
%Establish regularly spaced points
num_points = 20;
time_pts = linspace(min(time),max(time),num_points);
depth_pts = linspace(min(depth),max(depth),num_points);
%Run interpolation (with smoothing)
Pest = gridfit(depth, time, temp, time_pts, depth_pts);