Cut ultrasound signal between specific values using Octave - octave

I have an ultrasound wave (graph axes: Volt vs microsecond) and need to cut the signal/wave between two specific value to further analyze this clipping. My idea is to cut the signal between 0.2 V (y-axis). The wave is sine shaped as shown in the figure with the desired cutoff points in red
In my current code, I'm cutting the signal between 1900 to 4000 ms (x-axis) (Aa = A(1900:4000);) and then I want to make the aforementioned clipping and proceed with the code.
Does anyone know how I could do this y-axis clipping?
Thanks!! :)
clear
clf
pkg load signal
for k=1:2
w=1
filename=strcat("PCB 2.1 (",sprintf("%01d",k),").mat")
load(filename)
Lthisrun=length(A);
Pico(k,1:Lthisrun)=A;
Aa = A(1900:4000);
Ah= abs(hilbert(Aa));
step=100;
hold on
i=1;
Ac=0;
for index=1:step:3601
Ac(i+1)=Ac(i)+Ah(i);
i=i+1
r(k)=trapz(Ac)
end
end

ok, you want to just look at values 'above the noise' in your data. Or, in this case, 'clip out' everything below 0.2V. the easiest way to do this is with logical indexing. You can take an array and create a sub array eliminating everything that doesn't meet a certain logical condition. See this example:
f = #(x) sin(x)./x;
x = [-100:.1:100];
y = f(x);
plot(x,y);
figure;
x_trim = x(y>0.2);
y_trim = y(y>0.2);
plot(x_trim, y_trim);
From your question it looks like you want to do the clipping after applying the horizontal windowing from 1900-4000. (you say that that is in milliseconds, but your image shows the pulse being much sooner than 1900 ms). In any case, something like
Ab = Aa(Aa > 0.2);
will create another array Ab that will only contain the portions of Aa with values above 0.2. You may need to do something similar (see the example) for the horizontal axis if your x-data is not just the element index.

Related

How edge indexing works in graphhopper?

I'm here with a new question.
I'm making a custom algorithm that need precomputed data for the graph edges. I use the AllEdgesIterator like this :
AllEdgesIterator it = graph.getAllEdges();
int nbEdges = it.getCount();
int count = 0;
int[] myData = new int[nbEdges];
while (it.next())
{
count++;
...
}
The first weird thing is that nbEdges is equal to 15565 edges but count is only equal to 14417. How is it possible ?
The second weird thing is when I run my custom A* : I simply browse nodes using the outEdgeExplorer but I get an IndexOutOfBound at index 15569 on myData array. I thought that the edge indexes were included in [0 ; N-1] where N is the number of edges, is it really the case ?
What could be happening here ? By the way, I have disabled graph contraction hierarchies.
Thank you for answering so fast every time !
The first weird thing is that nbEdges is equal to 15565 edges
but count is only equal to 14417. How is it possible ?
This is because of the 'compaction' where unreachable subnetworks are removed, but currently only nodes are removed from the graph the edges are just disconnected and stay in the edges-'array' marked as deleted. So iter.getCount is just an upper limit but the AllEdgeIterator excludes such unused edges correctly when iterating and has the correct count. But using iter.getCount to allocate your custom data array is the correct thing to do.
Regarding the second question: that is probably because the QueryGraph introduces new virtual edges with a bigger edgeId as iter.getCount. Depending on the exact scenario there are different solutions like just excluding or using the original edge instead etc

Inverted Smoothstep?

I am currently trying to simulate ballistics on an object, that is otherwise not affected by physics. To be precise, I have a rocket-like projectile, that is following an parabolic arc from origin to target with a Lerp. To make it more realistic, I want it not to move at constant speed, but to slow down towards the climax and speed up on its way back down.
I have used the Mathf.Smoothstep function to do the exact opposite of what i need on other objects, i.e. easing in and out of the motion.
So my question is: How do I get an inverted Smoothstep?
I found out that what i would need is actually the inverted formula to smoothstep [ x * x*(3 - 2*x) ], but being not exactly a math genius, I have no idea how to do that. All I got from online calculators was some pretty massive new function, which I'm afraid would not be very efficient.
So maybe there is a function that comes close to an inverted smoothstep, but isn't as complex to compute.
Any help on this would be much appreciated
Thanks in advance,
Tux
Correct formula is available here:
https://www.shadertoy.com/view/MsSBRh
Solution by Inigo Quilez and TinyTexel
Flt SmoothCubeInv(Flt y)
{
if(y<=0)return 0;
if(y>=1)return 1;
return 0.5f-Sin(asinf(1-2*y)/3);
}
I had a similar problem. For me, mirroring the curve in y = x worked:
So an implementation example would be:
float Smooth(float x) {
return x + (x - (x * x * (3.0f - 2.0f * x)));
}
This function has no clamping, so that may have to be added if x can go outside the 0 to 1 interval.
Wolfram Alpha example
If you're moving transforms, it is often a good idea to user iTween or similar animation libraries instead of controlling animation yourself. They have a an easy API and you can set up easing mode too.
But if you need this as a math function, you can use something like this:
y = 0.5 + (x > 0.5 ? 1 : -1) * Mathf.Pow(Mathf.Abs(2x - 1),p)/2
Where p is the measure of steepness that you want. Here's how it looks:
You seem to want a regular parabola. See the graph of this function:
http://www.wolframalpha.com/input/?i=-%28x%2A2-1%29%5E2%2B1
Which is the graph that seems to do what you want: -(x*2-1)^2+1
It goes from y=0 to y=1 and then back again between x=0 and x=1, staying a bit at the top around x=0.5 . It's what you want, if I understood it correctly.
Other ways to write this function, according to wolfram alpha, would be -(4*(x-1)*x) and (4-4*x)*x
Hope it helps.

Freefem++: Solving poisson equation with numerical function

I am using Freefem++ to solve the poisson equation
Grad^2 u(x,y,z) = -f(x,y,z)
It works well when I have an analytical expression for f, but now I have an f numerically defined (i.e. a set of data defined on a mesh) and I am wondering if I can still use Freefem++.
I.e. typical code (for a 2D problem in this case), looks like the following
mesh Sh= square(10,10); // mesh generation of a square
fespace Vh(Sh,P1); // space of P1 Finite Elements
Vh u,v; // u and v belongs to Vh
func f=cos(x)*y; // analytical function
problem Poisson(u,v)= // Definition of the problem
int2d(Sh)(dx(u)*dx(v)+dy(u)*dy(v)) // bilinear form
-int2d(Sh)(f*v) // linear form
+on(1,2,3,4,u=0); // Dirichlet Conditions
Poisson; // Solve Poisson Equation
plot(u); // Plot the result
I am wondering if I can define f numerically, rather than analytically.
Mesh & space Definition
We define a square unit with Nx=10 mesh and Ny=10 this provides 11 nodes on x axis and the same for y axis.
int Nx=10,Ny=10;
int Lx=1,Ly=1;
mesh Sh= square(Nx,Ny,[Lx*x,Ly*y]); //this is the same as square(10,10)
fespace Vh(Sh,P1); // a space of P1 Finite Elements to use for u definition
Conditions and problem statement
We are not going to use solve but we ll handle matrix (a more sophisticated way to solve with FreeFem).
First we define CL for our problem (Dirichlet ones).
varf CL(u,psi)=on(1,2,3,4,u=0); //you can eliminate border according to your problem state
Vh u=0;u[]=CL(0,Vh);
matrix GD=CL(Vh,Vh);
Then we define the problem. Instead of writing dx(u)*dx(v)+dy(u)*dy(v) I suggest to use macro, so we define div as following but pay attention macro finishes by // NOT ;.
macro div(u) (dx(u[0])+dy(u[1])) //
So Poisson bilinear form becomes:
varf Poisson(u,v)= int2d(Sh)(div(u)*div(v));
After we extract Stifness Matrix
matrix K=Poisson(Vh,Vh);
matrix KD=K+GD; //we add CL defined above
We proceed for solving, UMFPACK is a solver in FreeFem no much attention to this.
set(KD,solver=UMFPACK);
And here what you need. You want to define a value of function f on some specific nodes. I'm going to give you the secret, the poisson linear form.
real[int] b=Poisson(0,Vh);
You define value of the function f at any node you want to do.
b[100]+=20; //for example at node 100 we want that f equals to 20
b[50]+=50; //and at node 50 , f equals to 50
We solve our system.
u[]=KD^-1*b;
Finally we get the plot.
plot(u,wait=1);
I hope this will help you, thanks to my internship supervisor Olivier, he always gives to me tricks specially on FreeFem. I tested it, it works very well. Good luck.
The method by afaf works in the case when the function f is a free-standing one. For the terms like int2d(Sh)(f*u*v), another solution is required. I propose (actually I have red it somewhere in Hecht's manual) an approach that covers both cases. However, it works only for P1 finite elements, for which the degrees of freedom are coincided with the mesh nodes.
fespace Vh(Th,P1);
Vh f;
real[int] pot(Vh.ndof);
for(int i=0;i<Vh.ndof;i++){
pot[i]=something; //assign values or read them from a file
}
f[]=pot;

Best Inverse kinematics algorithm with constraints on joint angles

I implemented the CCD algorithm for Inverse kinematics, it works great but it fails with constraints, I want to implement a system in which if the arm cannot reach the target it tries to get closer to it.
I tried to put the constraints in CCD algorithm, that is if my rotation angle goes above or below the constraint i limit it to max or min. for example, if the rotation angle is 100 degree and the constraint is 90, then I rotate 90 and based on that I calculate other angles, It works for some cases but fails for most.
CAn anyone tell me a 3D IK algorithm which takes care of constraints?
CCD itself will work like a charm.
If you are working on 3D, first find the rotation you should do in each axis without applying bone limits.
After that, the approach
expectedRotation.X = min(expectedRotation.X, maxLimit.X)
expectedRotation.X = max(expectedRotation.X, minLimit.X)
expectedRotation.Y = min(expectedRotation.Y, maxLimit.Y)
expectedRotation.Y = max(expectedRotation.Y, minLimit.Y)
expectedRotation.Z = min(expectedRotation.Z, maxLimit.Z)
expectedRotation.Z = max(expectedRotation.Z, minLimit.Z)
is wrong. Because, if you can't move in one of the axis further, the other two axis will keep moving and you will get weird results.
The fix:
If any of the 3 axis mismatches with the limit constraints, you must not change the rotation at all.
First convert all the angles into degrees in -180 to 180 format. Then the following will do
vector3df angleDifference = expectedRotation - baseRotation; //baseRotation is just the initial rotation from which the bone limits are calculated.
if(angleDifference.X < boneLimits.minRotation.X || angleDifference.Y < boneLimits.minRotation.Y || angleDifference.Z < boneLimits.minRotation.Z || angleDifference.X > boneLimits.maxRotation.X || angleDifference.Y > boneLimits.maxRotation.Y || angleDifference.Z > boneLimits.maxRotation.Z)
return currentRotation;
return expectedRotation;

Comparing a user generated line graph to one randomly generated by a program

The program will show the student a line graph. The student will have to recreate that line graph by moving a character away from or toward a motion detector using the arrow keys, creating a distance-time plot. I can capture the data points that the program generates when drawing its graph. I can also capture the data points gnerated by the student. How can I compare the two graphs while allowing for some tolerance on the student's part? Should I try to detect incorrect graphs as they are being drawn or after all data points are recorded? While some of the graphs will be linear and easy to compare others will be piecewise functions with positive, negative, and zero slopes at random intervals.
Thanks!
Does the order in which the graph lines are drawn matter ?
You could record the points with a certain threshold into an Array/Vector and compare.
A quick'n'dirty way would be using 2 binary(monochrome, just black and white) images:
One image will be a 'print screen'(BitmapData.draw()) of the graph(e.g. black on white)
The other image will be a white(blank) BitmapData that you'll use to write black pixels
where the user/student draws(has the mouse while it's pressed).
e.g.
userBitmapData.setPixel(mouseX,mouseY,0x000000);
When the drawing is complete(either the mouse is released or whatever rule you set),
you run a function that checks how much black pixels from the source(original graph) image
are matched in the destination(user graph) image.
Either you create a BitmapData containing the other two bitmaps blended on Difference mode, so anything that isn't black is not a match, or just loop through all the pixels once and manually check if the pixels match. Note that this relies on the fact that dimensions(width,height) of the two images are the same.
Here's a bit of code to illustrate this:
function compare(source:BitmapData,destination:BitmapData,threshold:Number):Boolean{
var commonPixels:Number = 0, totalPixels:Number = 0;
for(var j:int = 0 ; j < source.height ; j++){
for(var i:int = 0 ; i < source.width; i++){
pixels++;
if(source.getPixel(i,j) == destination.getPixel(i,j)) commonPixels++;
}
}
trace('matching: ' + (commonPixels/pixels * 100) + ' % ');//delete this line,just testing
if(commonPixels/pixels >= threshold) return true;
else return false;
}
//usage:
trace('is the graph correct ?: ' + compare(graphBitmapData,userBitmapData,0.7));
The Vector/Array version would be similar, but there would be no visual cues. Depending on your setup, you might want to test which would work best for you: BitmapData takes more memory than Arrays, but you can easily create a Bitmap, add it to the display list and check if looks right, etc.
If speed is an issue:
using Vector. instead of Array might be faster
looping in reverse(highest number to 0, decrementing) also should speed up things a bit
you probably get away with one loop instead of two
e.g.
var pixels:int = source.width * source.height;
for(pixels; pixels >=0; pixels--)
HTH