Difficulty plotting correct IFFT of 2D function from given data - matlab

I am trying to read in a 2D data set into a matrix, plot the matrix, as well as plot the IFFT of the matrix. The data is 128x2 data set, with frequency in the first column (A) vs amplitude in the second column (B)
Unfortunately, plotting the matrix of the data is not plotting the correct waveform. Also, the IFFT seems to be incorrect as well.
waves = csvread('10cm.txt');
A = waves(:,1);
B = abs(waves(:,2));
Matrix = [A B];
waves_transform = abs(ifft2(waves));
figure, plot(waves);
figure, plot(waves_transform)
When I read in each column of the data and plot A vs B, the waveform of the data is correct but the ifft2 of the data is incorrect. I need to properly take the inverse Fourier transform of the two dimensional data that I have read in.
waves = csvread('10cm.txt');
A = waves(:,1);
B = abs(waves(:,2));
Matrix = [A B];
waves_transform = abs(ifft2(Matrix));
figure, plot(A,B);
figure, plot(waves_transform)
waves & waves_transform
Does anyone know why reading in the data and plotting it is different than reading in each of the columns and plotting it results in different graphs? Also, can anyone help me take the IFFT of the 2D data correctly?
10cm.txt DATA FILE HERE: http://pastebin.com/0t0TwVvC

According to MATLAB documentation, if you do plot(Y) and Y is a matrix, then the plot function plots the columns of Y versus their row number. The x-axis scale ranges from 1 to the number of rows in Y.
So, in your case you have to do:
plot(waves(:,1), waves(:,2))
Might I also suggest a free and IMO better numpy package for python

Related

Plotting with a matrix in Matlab

The thing is i have a matrix and when i use imagesc() it goes like this but my goal is this.
So my question is does any one know which plot is this or some one has document about this, thanks.
If you have two vectors r and theta that give the polar coordinates, or two matrices rGrid and thetaGrid that give the polar coordinates for each element of the data matrix, then code like this will work:
r=linspace(1,20,20);
theta=linspace(0,2*pi,20);
data = r'.*sin(2.*theta); % INSERT DATA HERE
[thetaGrid,rGrid]=meshgrid(theta,r); % Create coordinate grid if needed
[xGrid,yGrid]=pol2cart(thetaGrid,rGrid);
surf(xGrid,yGrid,data); % Plot data
view(2);
Just keep in mind that the rows of the data matrix need to correspond to different radii, and columns need to correspond to different values of theta. If it's flipped, then transpose the matrix before plotting:
data = data';
Also, if the data doesn't wrap around from 0 to 2*pi radians, then repeat the first value of theta as the last value, and repeat the first column of the data matrix as a new final column:
theta(end+1)=theta(1);
data=cat(2,data,data(:,1));
There is also a 3D Polar Plot function on the MATLAB file exchange, but I do not have any experience using it: 3D Polar Plot

Taking IFFT2 of data set and what is means

I am dealing with a data set collected from a bistatic radar system which has electric field amplitude vs frequency points. I am trying to take the inverse fourier transform of the data set, converting from the frequency domain into the time domain. I have read in the data in the code below, created two different arrays, one for freq and one for amplitude. I correctly plot the data, but when I take the IFFT of the data I do not get what i expect.
Can someone tell me how to properly take the 2 dimensional fast fourier transform of the data set in matlab, and what exactly the IFFT in this case scenario is showing?
waves = csvread('10cm.txt');
A = waves(:,1);
B = abs(waves(:,2));
Matrix = [A B];
waves_transform = abs(ifft2(Matrix));
figure, plot(A,B), title('Frequency Domain'), xlabel('Frequency'),ylabel('amplitude');
figure, plot(waves_transform),title('Time Domain'), xlabel('Frequency'),ylabel('amplitude');
%axis([0 5 0 17*10^9]);
10cm.txt DATA FILE HERE: http://pastebin.com/0t0TwVvC
code output

How to create a 2D-matrix out of my data for surf()?

I have a 25000x3-matrix, with each row containing a x-, a y- and a z-value. Now I wanted to do a graphical plot out of these. But for using for example surf(Z) I have to use a mxn-matrix as Z with m equal the size of x and n equal the size of y. How can I reshape the matrix I have to the needed mxn-matrix? The problem is that my x- and y-values are no ints, but floats, so I assume that I have to do a interpolation first. Is that true? My data plotted with plot3 looks like:
The fact that your x- and y- values are not integers is not a problem at all. The real question is: are your (x,y) points forming a grid, or not ?
If your points are forming a grid, then you have to reshape your columns to form m-by-n arrays. You may need to sort your data according to the first, then second column and then use the reshape function.
If your points are not forming a grid, then you will have to make an interpolation. By chance the scatterinterpolant class can nicely help you in doing so.
As you can see, the data you are providing is neither given in a gridded way, nor is the point cloud clean. You could however try to do the following:
Project the point cloud onto the x-y plane
Triangulate those points
Give the points their original z-coordinate back.
Plot the surface using trisurf
Here is a MATLAB code that does this:
%// Generate some points P
[X,Y] = ndgrid(0:30);
P = [X(:), Y(:), X(:).^2+Y(:)];
%%// Here the actual computation starts
[~,I] = unique(P(:,1:2),'rows'); %// Remove points with duplicate (x,y)-coords
P = P(I,:);
T = delaunay(P(:,1),P(:,2)); %// Triangulate the 2D-projection
surface = triangulation(T, P(:,1), P(:,2), P(:,3)); %// Project back to 3D
trisurf(surface); %// Plot
You may want to remove stray points first, though.

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.

Matlab cdfplot: how to control the spacing of the marker spacing

I have a Matlab figure I want to use in a paper. This figure contains multiple cdfplots.
Now the problem is that I cannot use the markers because the become very dense in the plot.
If i want to make the samples sparse I have to drop some samples from the cdfplot which will result in a different cdfplot line.
How can I add enough markers while maintaining the actual line?
One method is to get XData/YData properties from your curves follow solution (1) from #ephsmith and set it back. Here is an example for one curve.
y = evrnd(0,3,100,1); %# random data
%# original data
subplot(1,2,1)
h = cdfplot(y);
set(h,'Marker','*','MarkerSize',8,'MarkerEdgeColor','r','LineStyle','none')
%# reduced data
subplot(1,2,2)
h = cdfplot(y);
set(h,'Marker','*','MarkerSize',8,'MarkerEdgeColor','r','LineStyle','none')
xdata = get(h,'XData');
ydata = get(h,'YData');
set(h,'XData',xdata(1:5:end));
set(h,'YData',ydata(1:5:end));
Another method is to calculate empirical CDF separately using ECDF function, then reduce the results before plotting with PLOT.
y = evrnd(0,3,100,1); %# random data
[f, x] = ecdf(y);
%# original data
subplot(1,2,1)
plot(x,f,'*')
%# reduced data
subplot(1,2,2)
plot(x(1:5:end),f(1:5:end),'r*')
Result
I know this is potentially unnecessary given MATLAB's built-in functions (in the Statistics Toolbox anyway) but it may be of use to other viewers who do not have access to the toolbox.
The empirical CMF (CDF) is essentially the cumulative sum of the empirical PMF. The latter is attainable in MATLAB via the hist function. In order to get a nice approximation to the empirical PMF, the number of bins must be selected appropriately. In the following example, I assume that 64 bins is good enough for your data.
%# compute a histogram with 64 bins for the data points stored in y
[f,x]=hist(y,64);
%# convert the frequency points in f to proportions
f = f./sum(f);
%# compute the cumulative sum of the empirical PMF
cmf = cumsum(f);
Now you can choose how many points you'd like to plot by using the reduced data example given by yuk.
n=20 ; % number of total data markers in the curve graph
M_n = round(linspace(1,numel(y),n)) ; % indices of markers
% plot the whole line, and markers for selected data points
plot(x,y,'b-',y(M_n),y(M_n),'rs')
verry simple.....
try reducing the marker size.
x = rand(10000,1);
y = x + rand(10000,1);
plot(x,y,'b.','markersize',1);
For publishing purposes I tend to use the plot tools on the figure window. This allow you to tweak all of the plot parameters and immediately see the result.
If the problem is that you have too many data points, you can:
1). Plot using every nth sample of the data. Experiment to find an n that results in the look you want.
2). I typically fit curves to my data and add a few sparsely placed markers to plots of the fits to differentiate the curves.
Honestly, for publishing purposes I have always found that choosing different 'LineStyle' or 'LineWidth' properties for the lines gives much cleaner results than using different markers. This would also be a lot easier than trying to downsample your data, and for plots made with CDFPLOT I find that markers simply occlude the stairstep nature of the lines.