Companding and uniform quantizaton in Matlab - matlab

I hava a function for quantization and decrease the bit depth of images and then increase it and compare with original image.As result I need a table like this:(MSE:mean square error)
but I don't know a much about companding and uniform qauntizations.
this is my function.
function [Q,Q2,Err] = quantization (I,b);
img=imread(I);
Q=uint8(img/2^(8-b));
Q2=uint8(Q*2^(8-b));
Err=mean(mean((double(img) - double(Q2)).^2,2),1);
subplot(1,4,1), imshow(img)
subplot(1,4,2), imshow(Q)
subplot(1,4,3), imshow(Q2)
display(Err)
end
help me please...

Related

Monte Carlo simulation for approximating delta in Matlab

I need to code the Monte Carlo algorithm for approximating delta to Matlab and calculate confidence intervals:
but for some reason my code doesn't work, any ideas why?
randn('state', 100)
%Problem and method parameters
S=10; E=9; sigma=0.1; r=0.06; T=1;
Dt=1e-3; N=T/Dt; M=2^17;h=10^(-4);
delta = zeros(M,1);
for i = 1:M
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T).*randn(M,1));
S_h = (S+h)*exp((r-0.5*sigma^2)*T+sigma*sqrt(T).*randn(M,1));
delta(i) = exp(-r*T).*(max(Sfinal-E,0)-max(S_h-E,0))/h;
end
aM=mean(delta);
bM=std(delta);
conf=[aM-1.96*bM/sqrt(M),aM+1.96*bM/sqrt(M)]
The error message is
"Unable to perform assignment because the left and right sides have a different number of elements."
Any help is appreciated!
You do not need to explicitly write the for loop since you have already vectorized it. In other words, Sfinal and S_h are vectors of length M, and their i-th entries correspond to S_i and S^h_i in the image. Since the right hand side of the delta expression evaluates to a vector of length M, which contains all the values of delta, you should assign that vector directly to delta, not delta(i).
One more thing: the pseudo-code in the image seems to suggest that the same random number should be used for calculating both S_i and S^h_i. This is not the case in your code, since you call randn separately for calculating Sfinal and S_h. I think you should generate the random samples once, save them, and then use it for both calculations.
Here's the code:
randn('state', 100)
%Problem and method parameters
S=10; E=9; sigma=0.1; r=0.06; T=1;
Dt=1e-3; N=T/Dt; M=2^17;h=10^(-4);
xi = randn(M,1);
Sfinal = S*exp((r-0.5*sigma^2)*T+sigma*sqrt(T).*xi);
S_h = (S+h)*exp((r-0.5*sigma^2)*T+sigma*sqrt(T).*xi);
delta = exp(-r*T).*(max(Sfinal-E,0)-max(S_h-E,0))/h;
aM=mean(delta);
bM=std(delta);
conf=[aM-1.96*bM/sqrt(M),aM+1.96*bM/sqrt(M)]

Matlab trouble with a lot of data. Vibration data is 3 million rows long

I have vibration data(g) in x and y direction for a Ball Bearing in 2 columns. Is there a way to find Manhattan distance just with this data & time?
If all you look for is summing data(:,1) and data(:,2), (as in #Austin answer) using vectored sum will be the fastest:
out = data(:,1)+data(:,2);
Here is an example:
function timming_mat_dist
data = randi(100,1e6,2);
timeit(#()arrmat(data))
timeit(#()vecmat(data))
end
function out = arrmat(data)
man_hat_dist_func = #(x,y)x+y;
out = arrayfun(man_hat_dist_func,data(:,1),data(:,2));
end
function out = vecmat(data)
out = data(:,1)+data(:,2);
end
and the results are 2.8936 seconds for arrmat and 0.0020463 seconds for vecmat.
However, if you want to compute all distances you should use pdist:
out = pdist(data,'cityblock');
but be aware that the output will be huge (and MATLAB probably won't let allocate so much memory for that) as #hammadian pointed in his answer.
It depends on which pair you want to measure the distance between. If you are looking for all possible combination of points, this will be memory and time consuming as the space requirement will be stored in 2D matrix of size:3 million * 3 million /2 ( /2 as distance between a and b is the same as b and a), so the matrix is upper triangular.
This is 3000000*3000000/2=4.5 Tera bytes. This can be improved but still will be very high in memory requirements.
If you are trying to find manhattan distance for each set of x and y, I would recommend using arrayfun
The usage is as follows:
man_hat_dist_func = #(x,y)x+y;
out = arrayfun(man_hat_dist_func,data(:,1),data(:,2));
OR
out = arrayfun(#(x,y)x+y,data(:,1),data(:,2));
This should be your fastest option to solve the distances. You should also consider pre-allocating memory for out using out = zeros(Length(data(:,1)),1);.

Optimization of Image Reconstruction Algorithm using Genetic Algorithm in Matlab

I'm trying to optimize an image reconstruction algorithm using genetic algorithm.I took initial population size as 10.I have an input image an 10 reconstructed image.fitness function is the difference between these two.That is
fitness_1 = inputimage - reconstructedimage_1;
fitness_2 = inputimage - reconstructedimage_2;
:
:
fitness_10 = inputimage - reconstructedimage_10;
I want to chose the best fitness population among them.But my fitness result is an image(matrix with intensity values).So how can I get a single fitness value for each population for doing crossover in the next stage.
Please help.Thanks in advance
You need to define a function which measures the quality of the match as a single scalar value. Actually you have a choice here - anything which could measure the closeness in a more-or-less-continuous manner would work. However, probably the simplest is mean squared error of each pixel value in the image.
Here's how I might do this for your first reconstruction:
fitness_1 = abs(inputimage - reconstructedimage_1).^2;
fitness_1 = sum( fitness_1(:) ) / numel( fitness_1 );

Calculating a interest rate tree in matlab

I would like to calibrate a interest rate tree using the optimization tool in matlab. Need some guidance on doing it.
The interest rate tree looks like this:
How it works:
3.73% = 2.5%*exp(2*0.2)
96.40453 = (0.5*100 + 0.5*100)/(1+3.73%)
94.15801 = (0.5*96.40453+ 0.5*97.56098)/(1+2.50%)
The value of 2.5% is arbitrary and the upper node is obtained by multiplying with an exponential of 2*volatility(here it is 20%).
I need to optimize the problem by varying different values for the lower node.
How do I do this optimization in Matlab?
What I have tried so far?
InterestTree{1}(1,1) = 0.03;
InterestTree{3-1}(1,3-1)= 2.5/100;
InterestTree{3}(2,:) = 100;
InterestTree{3-1}(1,3-2)= (2.5*exp(2*0.2))/100;
InterestTree{3-1}(2,3-1)=(0.5*InterestTree{3}(2,3)+0.5*InterestTree{3}(2,3-1))/(1+InterestTree{3-1}(1,3-1));
j = 3-2;
InterestTree{3-1}(2,3-2)=(0.5*InterestTree{3}(2,j+1)+0.5*InterestTree{3}(2,j))/(1+InterestTree{3-1}(1,j));
InterestTree{3-2}(2,3-2)=(0.5*InterestTree{3-1}(2,j+1)+0.5*InterestTree{3-1}(2,j))/(1+InterestTree{3-2}(1,j));
But I am not sure how to go about the optimization. Any suggestions to improve the code, do tell me..Need some guidance on this..
Are you expecting the tree to increase in size? Or are you just optimizing over the value of the "2.5%" parameter?
If it's the latter, there are two ways. The first is to model the tree using a closed form expression by replacing 2.5% with x, which is possible with the tree. There are nonlinear optimization toolboxes available in Matlab (e.g. more here), but it's been too long since I've done this to give you a more detailed answer.
The seconds is the approach I would immediately do. I'm interpreting the example you gave, so the equations I'm using may be incorrect - however, the principle of using the for loop is the same.
vol = 0.2;
maxival = 100;
val1 = zeros(1,maxival); %Preallocate
finalval = zeros(1,maxival);
for ival=1:maxival
val1(ival) = i/1000; %Use any scaling you want. This will go from 0.1% to 10%
val2=val1(ival)*exp(2*vol);
x1 = (0.5*100+0.5*100)/(1+val2); %Based on the equation you gave
x2 = (0.5*100+0.5*100)/(1+val1(ival)); %I'm assuming this is how you calculate the bottom node
finalval(ival) = x1*0.5+x2*0.5/(1+...); %The example you gave isn't clear, so replace this with whatever it should be
end
[maxval, indmaxval] = max(finalval);
The maximum value is in maxval, and the interest that maximized this is in val1(indmaxval).

Decrete Fourier Transform in Matlab

I am asked to write an fft mix radix in matlab, but before that I want to let to do a discrete Fourier transform in a straight forward way. So I decide to write the code according to the formula defined as defined in wikipedia.
[Sorry I'm not allowed to post images yet]
http://en.wikipedia.org/wiki/Discrete_Fourier_transform
So I wrote my code as follows:
%Brutal Force Descrete Fourier Trnasform
function [] = dft(X)
%Get the size of A
NN=size(X);
N=NN(2);
%====================
%Declaring an array to store the output variable
Y = zeros (1, N)
%=========================================
for k = 0 : (N-1)
st = 0; %the dummy in the summation is zero before we add
for n = 0 : (N-1)
t = X(n+1)*exp(-1i*2*pi*k*n/N);
st = st + t;
end
Y(k+1) = st;
end
Y
%=============================================
However, my code seems to be outputting a result different from the ones from this website:
http://www.random-science-tools.com/maths/FFT.htm
Can you please help me detect where exactly is the problem?
Thank you!
============
Never mind it seems that my code is correct....
By default the calculator in the web link applies a window function to the data before doing the FFT. Could that be the reason for the difference? You can turn windowing off from the drop down menu.
BTW there is an FFT function in Matlab