2D Discrete Cosine Transform using 1D DCT - matlab

I am trying to implement the 2D Discrete Cosine Transform to an image by using
1D DCT operations. My output is incorrect if I compare it to the dct2 MATLAB function. I don't understand what went wrong in my code and where it's happening.
If someone can point out the mistake or any other advice, that would be really helpful.
Here is my code written in MATLAB
% main function
signal=rand(100);
signal_dct=myDCT(signal);
figure; imshow((signal_dct));
% function to calculate 2D DCT of an image
function res=myDCT(signal)
signal=double(signal);
l=size(signal,1);
res=zeros(l); %initialize the final result matrix
for k=1:l %calculate 1D DCT of each row of image
res(k,:)=mdct(signal(k,:));
end
for k=1:l %calculate 1D DCT of each column of image
res(:,k)=mdct(res(:,k));
end
end
%% function to calculate 1D DFT of a 1D signal
function res=mdct(signal)
l=size(signal,1);
for i=1:l
if i==1 %for signal index of 1, alpha is 1/sqrt(l)
alpha=sqrt(1/l);
else %for signal index of greater than 1
alpha=sqrt(2/l);
end
j=[1:l];
% summation calculates single entry of res by applying the
% formula of DCT on the signal
summation=sum(sum(signal(j)*cos((pi*(2*(j-1)+1)*(i-1))/(2*l))));
res(i)=alpha*summation;
end
end

You are correct in that the 2D DCT is separable. You would just apply the 1D DCT to every row first, then take the intermediate result and apply it to the columns. However, you have two fundamental mistakes. Let's go through them.
Mistake #1 - Size of DCT isn't correct
Specifically, look at this statement in your mdct function:
l=size(signal,1);
Because you are applying the DCT for each row, then each column, the above would only work if you are applying the DCT to the columns. size(signal,1) would certainly give you the length of the input vector if the input was a column. However, if your input was a row, then the output of size(signal,1) would be 1. Therefore, you should replace size(signal,1) with numel so that you are for sure going to get the total number of elements - regardless if the input is a row or column.
Also, if you want to make the code compatible to do the summation in the DCT loop, you should make sure that the input is a row vector regardless. As such, do this instead:
l = numel(signal);
signal = signal(:).';
The first line determines how many elements we have for our input signal, and the second line ensures that we have a row vector. This is done by (:) to unroll the elements into a column vector, then doing .' after to ensure we transpose the result to get a row vector.
Mistake #2 - Summation statement isn't correct
Next, you're going to have to do element-wise multiplications in your summation to get what you're looking for. You also don't need the extra sum call there. It's superfluous. Therefore, modify your summation statement to this:
summation=sum(signal.*cos((pi*(2*(j-1)+1).*(i-1))/(2*l)));
There's no need to do signal(j) because j spans the entire length of the vector, and you can just do that with signal.
Once I made these changes, and I did this on a smaller size matrix to ensure that we get the same results:
rng(123123);
signal=rand(7);
signal_dct=myDCT(signal);
signal_dct2 = dct2(signal);
The last line of code calls dct2 so that we can compare the results from your custom function and what dct2 gives us.
We get:
>> signal_dct
signal_dct =
3.7455 -0.1854 -0.1552 0.3949 0.2182 -0.3707 0.2621
-0.2747 0.1566 -0.0955 0.1415 0.3156 -0.0503 0.8581
-0.2095 0.0233 -0.2769 -0.4341 -0.1639 0.3700 -0.2282
-0.0282 0.0791 0.0517 0.4749 -0.0169 -0.4327 0.0427
-0.4047 -0.4383 0.3415 -0.1120 -0.0229 0.0310 0.3767
-0.6058 -0.0389 -0.3460 0.2732 -0.2395 -0.2961 0.1789
-0.0648 -0.3173 -0.0584 -0.3461 -0.1866 0.0301 0.2710
>> signal_dct2
signal_dct2 =
3.7455 -0.1854 -0.1552 0.3949 0.2182 -0.3707 0.2621
-0.2747 0.1566 -0.0955 0.1415 0.3156 -0.0503 0.8581
-0.2095 0.0233 -0.2769 -0.4341 -0.1639 0.3700 -0.2282
-0.0282 0.0791 0.0517 0.4749 -0.0169 -0.4327 0.0427
-0.4047 -0.4383 0.3415 -0.1120 -0.0229 0.0310 0.3767
-0.6058 -0.0389 -0.3460 0.2732 -0.2395 -0.2961 0.1789
-0.0648 -0.3173 -0.0584 -0.3461 -0.1866 0.0301 0.2710
As you can see, both results match up. Looks good to me!
Just to be sure we are consistent, this is the full code listing for both your functions, with the modifications I made:
% function to calculate 2D DCT of an image
function res=myDCT(signal)
signal=double(signal);
l=size(signal,1);
res = zeros(l);
for k=1:l %calculate 1D DCT of each row of image
res(k,:)=mdct(signal(k,:));
end
for k=1:l %calculate 1D DCT of each column of image
res(:,k)=mdct(res(:,k));
end
end
%% function to calculate 1D DFT of a 1D signal
function res=mdct(signal)
%// Change
l = numel(signal);
signal = signal(:).';
for i=1:l
if i==1 %for signal index of 1, alpha is 1/sqrt(l)
alpha=sqrt(1/l);
else %for signal index of greater than 1
alpha=sqrt(2/l);
end
j=[1:l];
% summation calculates single entry of res by applying the
% formula of DCT on the signal
%// Change
summation=sum(signal.*cos((pi*(2*(j-1)+1).*(i-1))/(2*l)));
res(i)=alpha*summation;
end
end

Related

Encoding system of differential equations into a vector and approximate solutions

I am trying to solve a system of differential equations by the Euler's method. First, I have encoded my system into a vector. Then I pass the initial conditions to the function ode_Euler.
However, there's somehting wrong about my attempt. I am getting this error:
>> nm06p03a
Unable to perform assignment because the size of the left side is 1-by-2 and the size of the right side is 2-by-2.
Error in ode_Euler (line 11)
y(k+1,:)= y(k,:) +h*feval(f,t(k),y(k,:));
Error in nm06p03a (line 12)
tic, [tE,xE]=ode_Euler(f,tspan,x0,N); t_Euler=toc;
This is my code so far:
clear, clf
f=#(t,x)[-x(2)+x(1)*x(2); x(1)-(0.5.*x(1).^2)+(0.5.*x(2).^2)]; %Encoding system of differential equations into a vector
t0=0; tf=10;
tspan=[t0 tf];
N=100;
x0s=[0.2 0]; % A matrix consisting of initial points
for iter=1:size(x0s,1)
x0=x0s(iter,:);
tic, [tE,xE]=ode_Euler(f,tspan,x0,N); t_Euler=toc;
subplot(220+iter),
plot(tE,xE,'r:')
legend('ode_ Euler')
end
Here is the Euler's method:
function [t,y]=ode_Euler(f,tspan,y0,N)
if nargin<4|N<=0, N=100; end
if nargin<3, y0=0; end
h=(tspan(2)-tspan(1))/N;
t=tspan(1)+[0:N]'*h;
y(1,:)=y0(:)'; %make it a row vector
for k=1:N
y(k+1,:)= y(k,:) +h*feval(f,t(k),y(k,:));
end
When I use this other method ode_Heun, I get the same error:
function [t,y]=ode_Heun(f,tspan,y0,N)
if nargin<4|N<=0, N=100; end
if nargin<3, y0=0; end
h=(tspan(2)-tspan(1))/N; % Step-size
t=tspan(1)+[0:N]'*h; % Time vector
y(1,:)=y0(:)'; % make the initial value a row vector
for k=1:N
fk= feval(f,t(k),y(k,:)); y(k+1,:)= y(k,:)+h*fk; % Eq.(6.2.3)
y(k+1,:)= y(k,:) +h/2*(fk +feval(f,t(k+1),y(k+1,:))); % Eq.(6.2.4)
end
Can I get some help to understand the problem with my code?
y(k,:) is a row vector, while the return value of f is a column vector. Per broadcasting rules the sum of a row and a column vector is a matrix as the sum of the matrices of repeated row and column vectors.
This is not very logical in the context of vector and matrix operations, but can make sense in the processing of (finite) sequences of vectors. Unfortunately that distinction is not realized and enforced in the type system.

Problem in 2D heat equation solution using FDM in Matlab

I am trying to solve the 2D time dependent heat equation using finite difference method in Matlab. The code is below:
%Spatial variable on x direction
Lx=1;
delta=0.1;
xmin=-Lx/2;
xmax=Lx/2;
Nx=(xmax-xmin)/delta;
x=linspace(xmin,xmax,Nx);
%Spatial variable on y direction
Ly=1;
delta=0.1;
ymin=-Ly/2;
ymax=Ly/2;
Ny=(ymax-ymin)/delta;
y=linspace(ymin,ymax,Ny);
%Total matrix size
N = (Nx * Ny);
%Time variable
dt=0.002;
tmin=0;
tmax=1;
nt=(tmax-tmin)/dt;
tspan=linspace(tmin,tmax,nt);
%Create a meshgrid
[X,Y] = meshgrid(x,y);
% Defining initial state:
T0=exp(-(X.^2+Y.^2));
%reshape the initial condition to a vector
T_reshape = reshape(T0,N,1);
% Constructing the 1D spatial matrix
A=zeros(N,N);
I = eye(N);
%the diagonal elements
for m=1:N %the number of rows
for n=1:N %the number of columns
if (m==n)
A(m,n)=-2/delta^2;
end
%Boundary conditions: A(1,N)==A(N,1)
if(n==N)&&(m==1)
A(m,n)=1;
end
if(n==1)&&(m==N)
A(m,n)=1;
end
end
end
%the off-diagonal elements
for n=1:N-1
A(n+1,n)=1/delta^2; %the value of each lower off-diagonal elements
end
for n=2:N
A(n-1,n)=1/delta^2; %the value of each upper off-diagonal element
end
%create the 2D matrix
B = kron(A,I)+kron(I,A);
% Solve the equation
[Time,Tem]=ode45('dTDistribution',tspan,T_reshape,[],B,delta);
The function that is being called here is as following:
%Define the function
function dT=dTDistribution(tspan,T_reshape,dummy,B,delta)
dT = B.*T_reshape;
end
My problem is that the dimension of my matrix B is different than the dimensions of the initial condition T_reshape, therefore, the multiplication of B.*T_reshape won't be possible. I'm wondering how can I change the dimension of T_reshape to make the multiplication valid. Hope anyone could help.
Thank you.
Thank you for looking at my problem, but I have figured out the mistake in the code. Since A is the 1D matrix, then its size should be either (Nx,Nx) or (Ny,Ny) and then when taking the tensor product to get B the 2D matrix its size will be (N,N). However in the code, A has the size of (N,N) and as a result B is blowing up making the multiplication afterwards not possible.
Thanks.

When and how to do zero-padding for a discrete convolution?

I want to take the discrete convolution of two 1-D vectors. The vectors correspond to intensity data as a function of frequency. My goal is to take the convolution of one intensity vector B with itself and then take the convolution of the result with the original vector B, and so on, each time taking the convolution of the result with the original vector B. I want the final result to have the same length as the original vector B.
I am starting with a code in IDL that I am trying to modify to MATLAB. The relevant part of the code reads:
for i=1,10 do begin
if i lt 2 then A=B else A=Rt
n=i+1
Rt=convol(A,B,s,center=0,/edge_zero)
...
endfor
which I have rewritten in MATLAB
for i = 2:11
if i < 3
A = B; % indices start at 0, not 1
else
A = Rt;
end
n = i + 1;
% Scale by 1/s
Rt = (1/s).*conv(A,B);
...
end
But I am not sure how to incorporate the zero-padding that uses the edge_zero option. In IDL, the convolution calculates the values of elements at the edge of the vector as if the vector were padded with zeroes. The optional third option for the convolution function in MATLAB includes the option 'same', which returns the central part of the convolution of the same size as u for conv(u,v), but that doesn't seem to be the correct way about this problem. How do I do an analogous zero-padding in MATLAB?
Here is a code I needed for my doctoral research that I know does the zero padding correctly. I hope it helps.
function conv_out=F_convolve_FFT(f,g,dw,flipFlag),
if(nargin<4), flipFlag==0; end;
% length of function f to be convolved, initialization of conv_out, and padding p
N=length(f); conv_out=zeros(1,N); p=zeros(1,N);
% uncomment if working with tensor f,g's of rank 3 or greater
% f=F_reduce_rank(f); g=F_reduce_rank(g);
% padding. also, this was commented out: EN=length(fp);
fp = [f p]; gp = [g p];
% if performing convolution of the form c(w) = int(f(wp)g(w+wp),wp) = int(f(w-wp)gbar(wp),wp) due to reverse-order domain on substitution
if(flipFlag==1), gp=fliplr(gp); end;
% perform the convolution. You do NOT need to multiply the invocation of "F_convolve_FFT(f,g,dw,flipFlag)" in your program by "dx", the finite-element.
c1 = ifft(fft(fp).*fft(gp))*dw;
% if performing "reverse" convolution, an additional circshift is necessary to offset the padding
if(flipFlag==1), c1=circshift(c1',N)'; end;
% offset the padding by dNm
if(mod(N,2)==0), dNm=N/2; elseif(mod(N,2)==1), dNm=(N-1)/2; end;
% padding. also, this was commented out: EN=length(fp);
conv_out(:)=c1(dNm+1:dNm+N);
return

Vectors must be the same length error

I'm trying to plot an integral L, with respect to Xdot, but I keep getting vectors must be the same length error, I don't know how to fix it however. My code is shown below. You can see at the bottom, the loop for the sum only goes up to 99, while the x_1 goes up to 100. If I change the loop however, I get an error "index exceeds matrix dimensions"
% The solution for this part is based on the Euler method
f=0; %initializing the force row vector
f_1=0.5; %initializing the first derivative of force
x=1; % intializing the mass displacement%
x_1(1)=0; % initializing the first derivative of mass displacement
t=0; % initializing the time row vector
j=1; % initializing a`enter code here`n index used in iterations
a=0;
b=10;
N=100;
h=(b-a)/N;
for j = 0:N-1
f_2=-1*sin(f); %obtain the second derivative of the force
f_1=f_1+f_2*h; %obtain the first derivative of the force
f=f+f_1*h; % obtain the value of force and concatenate it with
%preceding force row vector the element
x_2=f-0.1*x_1-x-x^3; %obtain the second derivative of the mass displacement
x_1=x_1+x_2*h; % obtain the first derivative of the mass displacement
x=x+x_1*h; % obtain the current value of mass displacement and
%concatenate it with preceding mass displacement row vector the element
t=t+h; % obtain the current value of time and concatenate it with
%preceding time row vector the element
j=j+1; %increment the index iterator by one
v(j)=x;
w(j)=t;
m(j)=x_1
end
sum = 0; %%Trapezoidal method to find L, sum is L, put this at the end of your script
for i = 1:size(m,2)-1
sum = sum + h*(m(i+1)^2+m(i)^2)/2;
L(i) = sum
end
plot (m,L, 'r')
How about you shorten your x by one when plotting?
plot (m(1:end-1),L, 'r')

compute S transform and its square value in matlab

let us consider following code taken from
http://www.mathworks.com/matlabcentral/fileexchange/45848-stockwell-transform--s-transform-/content/stran.m
to compute S transform, here is my code
function ST=stran(h)
% Compute S-Transform without for loops
%%% Coded by Kalyan S. Dash %%%
%%% IIT Bhubaneswar, India %%%
[~,N]=size(h); % h is a 1xN one-dimensional series
nhaf=fix(N/2);
odvn=1;
if nhaf*2==N;
odvn=0;
end
f=[0:nhaf -nhaf+1-odvn:-1]/N;
Hft=fft(h);
%Compute all frequency domain Gaussians as one matrix
invfk=[1./f(2:nhaf+1)]';
W=2*pi*repmat(f,nhaf,1).*repmat(invfk,1,N);
G=exp((-W.^2)/2); %Gaussian in freq domain
% End of frequency domain Gaussian computation
% Compute Toeplitz matrix with the shifted fft(h)
HW=toeplitz(Hft(1:nhaf+1)',Hft);
% Exclude the first row, corresponding to zero frequency
HW=[HW(2:nhaf+1,:)];
% Compute Stockwell Transform
ST=ifft(HW.*G,[],2); %Compute voice
%Add the zero freq row
st0=mean(h)*ones(1,N);
ST=[st0;ST];
end
and consider following chirp signal
>> t = 0:0.001:2;
x = chirp(t,100,1,200,'quadratic');
i need confirmation that i am doing correctly following things
>> ST=stran(x);
>> plot(abs(ST))
?
picture is here
Posting my comment as answer:
I don't have much idea of the s-transform, but AFAIK the result of it is a 3D signal (as you can clearly see in the size of ST), so you may want to do imagesc(abs(ST)) or surf(abs(ST),'linestyle','none') instead of plot.
In your figure you have plotted 1000 lines, that's why its so chaotic.
Using
imagesc(abs(ST))