I have a dataset consisting of a position and a signal - the signal is sampled at scattered positions (0, 115, 230....):
0 1.709219858
115 1.676595745
230 1.643026005
345 1.609456265
460 1.574940898
575 1.540898345
690 1.506855792
806 1.473286052
I would like to smooth this data and then interpolate it to fill in the intervening positions i.e.:
0 x
1 x
2 x
3 x
4 x
5 x
6 x
7 x
8 x
9 x
10 x
Where x is the smoothed signal. I've been smoothing data with the commands:
>> hann250=hanning(250);
>> smooth250=conv(signal,hann250,'same');
But I am not sure at all how to interpolate the data - what commands can I use and what would I type? I'm totally new to MATLAB! I am also not sure what interpolation method I need but I intend to try various one's and see (once I know how!). Thanks,
T
You could try spline interpolation:
http://www.mathworks.com/help/matlab/ref/spline.html
% read x, y from your file
xx = linspace(min(x), max(x), 1000); % generate 1000 equally spaced points
yy = spline(x,y,xx); % interpolate
plot(x,y); % original
hold all;
plot(xx,yy); % new
You can use interp1:
data = [0 1.7092
115.0000 1.6766
230.0000 1.6430
345.0000 1.6095
460.0000 1.5749
575.0000 1.5409
690.0000 1.5069
806.0000 1.4733];
index_interp = 0:806; %// indices on which to interpolate
data_interp = interp1(data(:,1),data(:,2),index_interp,'linear');
There are other interpolation methods available in addition to 'linear'; see the above link.
Related
I do have generated two 2-D dimensional matrices from my core process. Now, I want to represent them via the bar graphs.
I could manage to get the 3d bar graph for independant matrix as shown in figures attached.
My data matrices are
"xData" - size is : (52 x 46 )
"yData" - size is : (52 x 46)
They'll always have the same size.
Now, I want to represent them together in 'Grouped Style' As show in here. I got the 3D dimensional matrix by combining them (xData and yData) together i.e. generated 52 x 46 x 2 matrix and then tried to plot with bar3 command; but, I got an error and couldn't plot.
Do you guys have any idea on how to do it ?
I manage to do that using a loop and this SO answer:
% generate random data
sz = [5 4];
xData = rand(sz);
yData = rand(sz);
% plot zeros in the same size to prepare plot
Z = zeros(sz);
h0 = bar3(Z);
set(h0,'EdgeColor','none','FaceColor','none');
axis tight
% plot each data column in a loop
hold on
for kk = 1:size(xData,2)
h1 = bar3([xData(:,kk),yData(:,kk)],'grouped');
% move current bars to their x position
cellfun(#(x) set(h1,'XData', x + (kk - 1)), get(h1,'XData'));
end
I am not familiar with MATLAB environment and I want to draw a matrix in a way that presents each cell of a matrix as a point in the 3D space.
For example present matrix "A " with the following points in 3D space:
x=1, y=1 Z= 10 , x=1, y=3 Z=27 until reach the point x=3, y=3, z=26.
A =
10 15 27
56 87 2
90 87 26
I do not want to use mesh and surf. I am looking for diagram like plot3 digram.I tried plot3 but it does not show the value of z correctly.
i=1:3;
j=1:3;
plot3(i,j,A(i,j))
In the above figure; 3 values are presented for when x=3 and y=3 however it should present these values for x=1,y=3; x=2,y=3;x=3,y=3
To begin I would recommend you to change your variable names. I'll use x and y instead of i and j. In many language these symbols are more typically used for scalar index rather than full vector indices, and in Matlab they can have a special significance (they are used to represent complex numbers).
That said, in your statement i=1:3; you only generate 3 indices, but you have 9 values to plot in your matrix. These 3 indices have to be repeated (3 times in your case, one for each column). So a proper x and y generation would be:
%% // Manual mesh/coordinate generation
x = bsxfun(#times,1:size(A,1), ones([size(A,2) 1])) ;
x = x(:) ;
y = bsxfun(#times, ones([1 size(A,1)]) , (1:size(A,2)).' ) ;
y = y(:) ;
With that you can use at your convenience scatter3 or plot3:
hscat = scatter3( x, y, A(:) ) ;
hp3 = plot3( x, y, A(:),'Marker','o','LineStyle','none') ;
%// will both produce exactly the same result
Now please consider the fact that the way I generated the x and y coordinate is nothing more than what meshgrid would do for you (or the more dimension generic ndgrid).
For example, in the code below, the 3 plotting methods will produce exactly the same ouput than above, so just take your pick:
%% define a grid
[X,Y] = meshgrid( 1:size(A,1) , 1:size(A,2) ) ;
%% // surface plot (but only points visible, no line)
hsurf = surf(X,Y,A,'Marker','o','LineStyle','none','FaceColor','none') ;
%% // scatter3
hscat = scatter3( X(:), Y(:), A(:) ) ;
%% // plot3
hp3 = plot3( X(:), Y(:), A(:),'Marker','o','LineStyle','none') ;
Why reinvent the wheel when you have one at hand ... meshgrid do the job for you in less code instruction ;-)
To generate the coordinates, you should use ndgrid (or meshgrid, which swaps X and Y.):
[X, Y] = ndgrid(1:3, 1:3);
plot3 is going to connect the input points with a line, so if you want distinct points in your plot, use scatter3:
scatter3(X(:), Y(:), A(:));
(You can also use plot3 in the same way if you want the lines.)
I usually use surf function to plot 3D figures in matlab, but now the data is different, so I am using plot3 and I have the below figure. Do you have any idea how I reconstruct this figure to be more understandable even if by using different function.
To be more concise, I have X values, with each X value there is a value of Y and value of Z.
X = [ 1 ;2 ;4; 8; 16; 32; 64];
Z = [ 1; 1.8 ; 3.46 ; 6.74 ; 13.18 ; 24.34 ; 39.33]
Y = [0 ; 56.92 ; 91 ; 109.95 ; 119 ; 123.57 ; 125.51]
fig = plot3(log(X),Y,Z,'b.-');
XLABEL=[ 1 2 4 8 16 32 64];
set(gca,'XTickLabel',XLABEL);
set(gca,'XTick',log(XLABEL));
YLABEL= [ 0 30 60 90 120 150 180];
set(gca,'YTickLabel',YLABEL);
set(gca,'YTick',YLABEL);
ZLABEL= [0 5 10 15 20 25 30 35 40 45 50 55];
set(gca,'ZTickLabel',ZLABEL);
set(gca,'ZTick',(ZLABEL));
ylim([0 180]);
zlim([0,55]);
grid on
It's difficult to say, because we don't have a context. Common options are:
Plotting x/y and x/z in two separate plots. Precisely readable but difficult to get the connection between y and z. subplot
Plotyy, same as previous but in one plot. Y and Z values which correspond to the same x-value are aligned. plotyy
Use a plot3 as shown above, but connect each point to the x/z plane. (details below)
Project the line on one or multiple planes and draw it there. (Plot the line again, setting x, y or z to 7 0 or 180, which is the location of your axis)
If two axis are of major importance, use a simple 2d plot and represent the third dimension using color/dotsize/annotations etc...
Code for Option 3:
At the end of your code, add the following code:
X2=[X';X';nan(size(X'))];
X2=X2(:);
Y2=[Y';Y';nan(size(Y'))];
Y2=Y2(:);
Z2=[Z';zeros(size(Z'));nan(size(Z'))];
Z2=Z2(:);
hold on
plot3(log(X2),Y2,Z2,'--')
To understand it, you have to know that matlab skips nans while plotting. Thus the code above generates a independent line segment for each point, connecting it to the ground plane.
I have a matrix
b = [1+ 1i, 2 + 1i, 2+ 2i, 3 + 3i, 3+ 3i ; ...
1.2 + 2i , 2+2i, 2.1 + 2.1i, 3+2.1i, 3.1 + 3.2i]
where real(b) is the x coordinate, b(x,:) is one experiment, and imag(b) is the y coordinate.
I want two things:
plot my experiments in a 2d plot as lines (but the points have to be in the right order)
plot my y (usually calledz) coordinate as a surface over the axes x and experiment.
The problem is, that I want lines along the rows and Matlab mixes the coordinates of the complex numbers up and the line appears in a zig-zag all over the place.
The more basic problem is that I want to have bars from x1 to x2 at y1 and I only came up with adding a data point y1 at x1 and x2. But at x2 there is also y2 which seems to confuse Matlab.
You can use Euler's formula to convert your data from Cartesian coordinates to polar coordinates.
clear all; close all;
function [rho, theta] = polarize(z)
rho = abs(z);
theta = angle(z);
end
b = [1+ 1i, 2 + 1i, 2+ 2i, 3 + 3i, 3+ 3i;
1.2 + 2i , 2+2i, 2.1 + 2.1i, 3+2.1i, 3.1 + 3.2i];
[rho1, theta1] = polarize(b(1,:));
[rho2, theta2] = polarize(b(2,:));
figure
hold on
polar(theta1, rho1, 'b');
polar(theta2, rho2, 'r');
print('-dpng','euler.png')
Result in Octave:
For question (1), plot(b) is going to give you lines made up of the columns of b. If you switch to using b-transpose, i.e. plot(b'), you'll plot each row separately.
plot(b')
ylim([-4 0])
xlim([-0 4])
Question (2) requires a certain toolbox for the resp function?
I am new to this forum, so please bear with me.
I have been working on this Matlab problem for a while now:
I have a digital elevation model (DEM) new_sub(x,y) in tif format. So it is a x-by-y matrix containing heights (z). I wish to resample parts of this DEM in different resolutions and restore this in another matrix. So far I have been working with for loops to change the resolution of different areas of the DEM and then wrote the results to an xyz-file:
x y z
1 1 123
1 2 233
1 3 231
2 1 235
2 2 531
2 3 452
and so forth.
Here is the code:
xmax = size(new_sub,2);
ymax = size(new_sub,1);
for k=1:200 % y
for l=1:xmax % x
fprintf(fid, '%d %d %d \n',l,xmax+1-k,new_sub(k,l));
end
end
% 1:4
for k=200/2+1:size(new_sub,1)/2
for l=1:size(new_sub,2)/2
fprintf(fid, '%d %d %d \n',l*2,ymax+2-k*2,new_sub(k*2,l*2));
end
end
This does work, but seems to be rather complicated. Moreover, it does not allow me to store the resampled areas in a single matrix within Matlab.
Is there a more efficient way of resampling certain areas of a Matrix with different resolutions, writing them into a new Matrix containg all resampled areas and then writing it to a file? I was looking into repmap, but could not think of a clever way of using it!
Your help is much appreciated!
THeo
To re-sample a matrix in Matlab:
For example matrix M:
M = [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];
If we wanted to sample on every nth pixel, it is as simple as this:
m = M(1:n:end, 1:n:end)
So for n=2
m = 1 3 5
11 13 15
21 23 25
I suggest you read up on indexing in matlab and also on using the colon operator to create vectors in matlab
Now in order to get in the "x y z" format you mentioned, first use meshgrid to generate matrices of X and Y coordinates.
[X, Y] = meshgrid(1:n:size(M,1), 1:n:size(M,2))
notice I use n to downsample X and Y. Now you just need to flatten the three matrices and combine them:
final = [X(:), Y(:), m(:)]
Finally to save as a file I suggest you type help save or help dlmwrite in the Matlab command promt and use either of those functions to save final
To me the easiest way to do looks like using imresize. You can treat your elevation map as an image I. Then you can cut sections out by indexing and rescaling as follows:
I = imread('my.tiff'); % read
section = I(1:200, :); % cut the first 200 rows and all columns
sectionResized = imresize(section, [numrows numcols]) % resample
imwrite(sectionResized, 'mynew.tiff'); % save