How to automatically calibrate axes in MATLAB? - matlab

So I am doing some ECG analysis in MATLAB and so far I have detected the key features as shown in the figure below :
This is the ground truth :
So how do i replot the first figure such that the x axis ranges from 0 to 10 (as shown in the second image)
I want to do this so that I can measure the time duration between the Q(the red cross) and the S (the peak at the minimum marked with a circle).
So essentially I want to
1) Calibrate 3600 samples into 10 seconds
2) Using the above scaling factor, be able to automatically calibrate any number of samples into the relevant seconds.
Thanks.

How did you plot the first figure? If you did not provide any x-axis, as in plot(ecg), the x-tick labels will be enumerated consecutevly in intervals of one. If you do know the corresponding time points t of ecg, you can use plot(t, egg);.
Since you know the sampling rate and assume consistent intervals between each data point, you can generate t yourself by t = 10.0 / 3600 * (0 : length(ecg)-1). This will create an array with length(ecg) elements starting at 0, with 10.0 per 3600 data points, for any length of ecg.
To align the horizontal axis limits more nicely than in the first plot, you can then also use xlim([0, t(end)]).

Related

Matlab - Set xticks for a secondary axis not even with the first axis

I have a function that create the cumulative probability that a certain time is kept (value between 0 and 1 on the Y-axis, and amount of days on the x-axis). Since I know the distance, I thought about adding a secondary x-axis with the average speed that the time correlates to.
Adding a secondary axis was relative easy like this example, as start I manage to set the xlim correct and also to reverse it.
xlabel('Time (days)')
xlim([27 38])
a1Pos = get(gca,'Position');
b = axes('Position',[a1Pos(1) a1Pos(2)-.06 a1Pos(3) a1Pos(4)], 'YTick',[],'YTickLabel',[]);
xmaxb = round(dist/(xmaxa*24));
xminb = round(dist/(xmina*24));
set(b,'Units','normalized');
set(b,'Color','none');
set(b, 'XDir','reverse')
set(b,'xlim',[xminb xmaxb])
xlabel(b,'Average speed on the journey (knots)')
There are two problems, first the rounding have too big impact and second, and more important it isn't a linear correlation between xminb and xmaxb, so I have to add the xticks manually? I tried to do it like this:
set(b,'XTick',[12 13 14 15 16 17])
Hence the now the secondary axis became empty and I don't know how to set different spacing between them (the distance between 12-13 to the right should be larger than between 16 and 17 to the left..)
I would use the same 'XTick' and 'Xlim' as in the main axes, and change the 'XTickLabel' property. You can assign any string (or number) to each tick mark.
But be explicit about 'XTick' when you define 'XTickLabel', so the labels are associated to specific locations on the axis. Scaling the figure can change the number of automatically generated tick marks, which would cause your selected labels to appear in the wrong locations.
I thought I post the solution, at little bit like Cris Luengo says, hence quite tricky (IMO) to get correct with decreasing values of the speed (with increased time):
xmaxb = floor(dist/(xmaxa*24)); %xmaxa = max value of the first xlabel
xminb = ceil(dist/(xmina*24)); %xmina = min value of the first xlabel
bticks_lab = fliplr(xmaxb: 0.5:xminb); % spacing 0.5 for the second axis and flip it.
bticks_loc = (dist./(bticks_lab * 24) - xmina) / (xmaxa-xmina);
% The new "b" axis got locations between 0 and 1, this took awhile to figure out!
b = axes('Position',[a1Pos(1) a1Pos(2)-.06 a1Pos(3) a1Pos(4)], 'YTick',[],'YTickLabel',[]);
set(b,'Units','normalized');
set(b,'Color','none');
set(b,'XTick',bticks_loc)
set(b,'XTickLabel',{bticks_lab})
Hope that it helps someone else in the future!

Making a sinusoidal curve in an image into a straight line in the output image

I have to make a sinusoidal curve in an image to output an equal straight line in the resulting image.
An example of input sinusoidal image:
What I think is one solution should be:
Placing down the origin of x and y coordinates at the start of the curve, so we will have y=0 at the starting point. Then points on the upper limit will be counted as such that y= y-(delta_y) and for lower limits, y=y+(delta_y)
So to make upper points a straight line, our resulting image will be:
O[x,y-delta_y]= I[x,y];
But how to calculate deltaY for each y on horizontal x axis (it is showing the distance of curve points from horizontal axis)
Another solution could be, to save all information of the curve in a variable and to plot it as a straight line, but how to do it?
Since the curve is blue we can use information from the blue and red channels to extract the curve. Simply subtraction of red channel from blue channel will highlight the curve:
a= imread('kCiTx.jpg');
D=a(:,:,3)-a(:,:,1);
In each column of the image position of the curve is index of the row that it's value is the maximum of that column
[~,im]=max(D);
so we can use row position to shift each column so to create a horizontal line. shifting each column potentially increases size of the image so it is required to increase size of the image by padding the image from top and bottom by the size of the original image so the padded image have the row size of 3 times of the original image and padding value is 255 or white color
pd = padarray(a,[size(a,1) 0 0], 255);
finally for each channel cirshift each column with value of im
for ch = 1:3
for col = 1: size(a,2)
pd(:,col,ch) = circshift(pd(:,col,ch),-im(col));
end
end
So the result will be created with this code:
a= imread('kCiTx.jpg');
D=a(:,:,3)-a(:,:,1);
%position of curve found
[~,im]=max(D);
%pad image
pd = padarray(a,[size(a,1) 0 0], 255);
%shift columns to create a flat curve
for ch = 1:3
for col = 1: size(a,2)
pd(:,col,ch) = circshift(pd(:,col,ch),-im(col));
end
end
figure,imshow(pd,[])
If you are sure you have a sinusoid in your initial image, rather than calculating piece-meal offsets, you may as well estimate the sinusoidal equation:
amplitude = (yMax - yMin)/2
offset = (yMax + yMin)/2
(xValley needs to be the valley immediately after xPeak, alternately you could do peak to peak, but it changes the equation, so this is the more compact version (ie you need to see less of the sinusoid))
frequencyScale = π / (xValley - xPeak)
frequencyShift = xFirstZeroCrossRising
If you are able to calculate all of those, this is then your equation:
y = offset + amplitude * sin(frequencyScale * (x + frequencyShift))
This equation should be all you need to store from one image to be able to shift any other image, it can also be used to generate your offsets to exactly cancel your sinusoid in your image.
All of these terms should be able to be estimated from the image with relatively little difficulty. If you can not figure out how to get any of the terms from the image, let me know and I will explain that one in particular.
If you are looking for some type of distance plot:
Take your first point on the curvy line and copy it into your output image, then measure the distance between that point and the next point on the (curvy) line. Use that distance to offset (from the first point) that next point into the output image only along the x axis. You may want to do it pixel by pixel, or grab clumps of pixels through averaging or jumping (as pixel by pixel will give you some weird digital noise)
If you want to be cleaner, you will want to set up a step size which was sufficiently small to approximately match the maximum sinusoidal curvature without too much error. Then, estimate the distance as stated above to set up bounds and then interpolate each pixel between the start and end point into the image averaging into bins based on approximate position. IE if a pixel from the original image would fall between two bins in the output image, you would split it and add its weighted parts to those two bins.

Line increment in gnuplot

I have large files (~5 Gbs) whit constant increment on x-axis, let's say each dt.
I would like to know if I could set the every command of Gnuplot as logarithmic increment not linear.
plot "fileA.txt" u 1:2 every dt #linear increment of dt
This is because, if x-axis is in log-scale, then I want to have more points for low values of x in (10^-4,10^-2) but also not an oversampling in (10^4,10^2) range. Somehow a differential increment.
Does I have to use external programs like sed to re-write my file first?
A test plot is included as well as the data. In blue the full data, in red the ones with the every command. As you can see one loose the information for short x also oversample the plot for large x. the data file
Many thanks.
You could plot smoothed data with points:
set key left
set logscale x
set yrange [3.9:4.8]
set samples 30
set terminal png
set output "log.png"
plot "fort.11" title "raw" with points lc 3 pointtype 5 pointsize 2,\
"" title "smooth" smooth csplines with points lc 1 pointtype 5 pointsize 1
set samples 30 tells gnuplot to use 30 points equidistant in x
smooth csplines interpolates the datapoints
with points plots with points instead of lines, which would be the default
Note that this does not plot the original data, and that smooth csplines introduces new points if the original datapoints are too far apart. This might or might not be what you want.

How to have a log scale plot so that the smallest value on the vertical axis is a power of 10

I have the following logarithmic plot shown below:
I want to change this plot so that the "x axis" is such that the vertical value lies at the smallest possible power of 10. What I mean by this is that I would like to make sure that the horizontal axis seen at the bottom of the plot is perhaps y = 10e-2 such that the rightmost group of bars in the above plot can be above the "x axis". I tried 'xaxislocation' but it doesn't work. In hindsight, I suppose the y=10e0 line is not the x axis anyway.
% plot group_err
data_names = cell(1,8);
data_names{1}='1'; data_names{2}='2';
data_names{3}='3'; data_names{4}='4';
data_names{5}='5'; data_names{6}='6';
data_names{7}='7'; data_names{8}='8';
h = bar(group_err);
grid on;
set(gca,'xticklabel',data_names,'YScale','log','FontSize',14);
ylabel('Error rate (%))','FontSize',14);
xlabel('Dataset','FontSize',14);
title('Error rate of sequential algorithms','FontSize',14);
ylim([0.01 100]);
group_err:
79.0407673860911 80.6235000000000 80.3837000000000
28.2600000000000 24.3600000000000 25.0200000000000
2.18055555555556 1.44290190000000 1.92145600000000
34.1692954784437 14.9053400000000 17.9127200000000
0.0551724137931035 0.0298850500000000 0.0459770500000000
33.2005921539600 22.4352400000000 25.6802200000000
0.0979391960824322 0.0685568400000000 0.155070440000000
Now that we've seen your edit, that's very straight forward. Simply find whatever y value is the smallest and you need to round this down so that the resulting value is a power of 10 and is smaller than the smallest y value you're looking at.
To do this, you want to the floor of the following relationship where given your minimum value ymin, it satisfies this relationship:
10^floor(x) = ymin
Re-arranging this equation by taking the log of both sides, we get:
x = log(ymin) / log(10)
... and we now take the floor of x to get what you need. Take special note that you need to take the floor as it rounds down to minus infinity. Don't use fix as this rounds towards 0 so for negative values, this will add 1 to negative values and not what you want. Specifically, this will ensure that you find the smallest power x that respects negative powers when the above relationship is less than 1.
The value of x serves as the smallest power of 10 that satisfies what you need. You will the need to take 10^x to complete the task. This is the smallest power of 10 that will serve as the horizontal axis of your plot. You then use ylim to limit the vertical axis so that you see what the smallest and largest values you have. Because you are using a semi-logarithmic plot, to do what you need you must specify these values as powers of 10. This is the whole reason why we need to determine the smallest power of 10 to serve as the minimum limit or the x axis of your data.
Therefore, assuming you have your plot already open, simply do the following:
x = floor(log(min(y)) / log(10));
ylim([10^x max(y)]);
ylim takes two values: The minimum value and maximum value of the y axis you would like to see. I've made sure that the largest value to visualize is just the largest value in your data itself.
what you want in to change the 'BaseValue' property of your bar plot, in your case would be:
set(h,'BaseValue',0.01)
You will get something like this:

plotting data between concentric circles of known radii

I have a vector of 1000 random numbers biased towards the bounds of 540 and 600. I need to plot this data as a wriggly circular path between two concentric circles of radii 540 and 600 respectively. How do I do this?
Presently, I'm able to plot the concentric circles of given radii, but if I try to plot the given random data which is between the bounds 540 and 600, it is plotted along the width between the two concentric circles. I want it to be plotted as a noisy circular curve between the concentric circles.
I hope I'm able to explain my point
If anyone can tell me, how do I do that. Thanks
Here is the link to my previous post, wherein I had to generate random numbers biased towards the two well defined bounds
Generating random numbers in matlab biased towards the boundaries
Now I need to plot the same data, as explained above.
This is the image I get:
What you actually want is a circular plot, so the first idea that comes to my mind is to use polar coordinate.
First we get your sample of around 1000 random datas biased to the bounds of your [540 600] interval following that.
Note that this algorithm will not always get you exactly 1000 values as the out of bound values are removed.
So we'll do something like this :
%Get the size of the sample
P=length(X);
% Generate P Angles uniformly distributed between 0 and 2*pi*(P-1)/P
% Note generating an angle equal to 0 and an angle equal to 2*pi would
% mean that we have 2 points with the same angle, and that s something
% you generally want to avoid
Angles=(0:P-1)*(2*pi/P);
% Plot your points with the markers (polar coordinates) in '-+',
% - means that you want a line to be drawn between the points and
% + means that you want a + marker on every point
plot(X.*cos(Angles),X.*sin(Angles),'-+');
%tell matlab to wait so you can add the circles
hold on
% add circle of 540 radius
plot(540*cos(Angles),540*sin(Angles),'-r');
%add circle of 600 radius
plot(600*cos(Angles),600*sin(Angles),'-r');
For the final result of :
Update : About getting the angles randomly distibuted
We'll have to define our Angles diffrently. We still need a vector of length P, but randomly distributed. This can be achieved by taking P random numbers between 0 and 1, and then multiply this vector by 2*pi in order to get randomly distributed values between 0 and 2*pi.
Last step is to order this vector in order to keep the ordering of your data.
Temp=rand(1,P)*2*pi;
Angles=sort(Temp);
For a final result of :
Another lead woult be to take a sample of 1000 gaussian random numbers and use them modulo 2*pi :
Angles=sort(mod(randn(1,P)*2*pi,2*pi));