I want to use CS to reconstruct an image from fewer samples.
I use Gaussian random matrix as measurement matrix. My problem is with Psi matrix which I want to be Haar wavelet coefficients but I don't know how to define it.
I have used DCT and Fourier basis and it worked well. Here is my code with Fourier basis.
Can anyone tell me how to define Psi matrix as haar wavelet transform?
Thanks in advance.
clc
clear all
close all
[fn,fp]=uigetfile({'*.*'});
tic
A=im2double(rgb2gray(imread([fp,fn])));
figure(1),imshow(A)
xlabel('original')
x=A(:);
n=length(x);
m=1900;
Phi=randn(m,n); %Measurment Matrix
Psi=fft(eye(n)); %sensing Matrix( or can be dct(eye(n)) )
y=Phi*x; %compressed signal
Theta=Phi*Psi;
%Initial Guess: y=Theta*s => s=Theta\y
s2=Theta\y;
%Solution
s1=OMP( Theta, y, 1e-3);
%Reconstruction
x1=Psi*s1;
figure,imshow(reshape(x1,size(A))),xlabel('OMP')
toc
You just need to generate a haar matrix of appropriate dimensions. Consider this MATLAB function:
function [h]=haargen(N)
% Generating Haar Matrix
ih=zeros(N,N);
h(1,1:N)=ones(1,N)/sqrt(N);
for k=1:N-1
p=fix(log(k)/log(2));
q=k-(2^p);
k1=2^p; t1=N/k1;
k2=2^(p+1); t2=N/k2;
for i=1:t2
h(k+1,i+q*t1) = (2^(p/2))/sqrt(N);
h(k+1,i+q*t1+t2) =-(2^(p/2))/sqrt(N);
end
end
Related
I have a data which contains 16 elements:
x=[8.57837e-08, 2.07482e-06, 4.43796e-06, 7.66462e-06, 1.10232e-05, 1.35811e-05, 1.27958e-05, 5.94217e-06, 2.49168e-08, -6.58389e-06, -1.30551e-05, -1.345e-05, -1.07471e-05, -7.38637e-06, -4.42876e-06, -1.88811e-06 ];
A = length(x)
I do DTFT-DFT like dirac signals:
n=0:A;
syms w
X_w=0;
for i=1:length(x)
X_w=X_w+x(i)*exp(-j*w*n(i));
end
figure;fplot(angle(X_w),[0 2*pi]),title('DTFT phase graph')
figure;fplot(abs(X_w),[0 2*pi]),title('DTFT amplitude graph')
hold on
%DFT
N=50;
k=0:N-1;
DFT_X=[];
for k=0:N-1
Xk=0;
for i=1:length(x)
Xk=Xk+x(i).*exp(-j*(2*pi/N).*k.*n(i));
end
DFT_X=[DFT_X Xk];
end
w=2*pi/N*(0:N-1);
stem(w,abs(DFT_X))`
The problem is I want to write this signal with cosinus and sinus components. But I don't really know how can I do.
Thank you all,
Emre.
Direct computation of the Fourier coefficients might be a better option than trying to relate the DFT to the DFS. Looking at the continuous time formulas:
all you'd need to do is sum the input signal x multiplied (element wise) by a cosine over it's domain for the real coefficient and sum the input signal x multiplied (element wise) by a sine over its domain for the imaginary coefficients.
Secondly, you could potentially use conjugate symmetry and these formulas to calculate the relationship between your Xk and the desired An and Bn coefficients.
Xk = (An-j*Bn)/2
and
Xk* = (An+j*Bn)/2)
I am trying to get the filter coefficients for a digital IIR filter of a simple 180° phase shift allpass filter with the transfer function: (1+s)/(1-s)
This is what Wolfram gives me:
Bode Plot in Wolfram
and this is what I get from MATLAB:
Bode Plot in MATLAB
My code is:
clc; clear; close all;
z = [-1]; %zeros
p = [1]; %poles
k = 1; %gain
[num,den] = zp2tf(z,p,k); %convert zero-pole into numerator denominator
freqz(num,den); %bode plot
I would like to get the same plot in MATLAB as I do in Wolfram Alpha, to obtain the filter coefficients with fvtool so I can write a filter in C. Therefore my question is how do manage to convert the poles and zeros of the transfer function into the right format so that MATLAB does the same plot like Wolfram Alpha? What am I doing wrong?
Your issue is that you are mixing concepts
freqz is for z-based discrete frequency transforms, while you are working with s-based continuous Laplace transforms. These are unequivocally not the same thing.
Just use the functions for continuous transforms.
z = [-1]; %zeros
p = [1]; %poles
k = 1; %gain
[num,den] = zp2tf(z,p,k); %convert zero-pole into numerator denominator
my_filter=tf(num,den);
bode(my_filter);
I have created this code in order to calculate and sketch the Fourier Coefficients A0 ,An ,and Bn without using Fourier Series command but,unfortunately the plots results did not look like a Fourier Series. if someone please guide me to solve this problem.
thank you
I identified all the terms that i am going to use in this project
T=1; %Time Period
wo=2*pi/T;
a0=0;% coefficient
A0=0;% coefficient
a1=0;% coefficient
A1=0;% coefficient
a2=0;% coefficient
A2=0;% coefficient
a3=0;% coefficient
A3=0;% coefficient
a4=0;% coefficient
A4=0;% coefficient
a5=0;% coefficient
A5=0;% coefficient
a6=0;% coefficient
A6=0;% coefficient
a7=0;% coefficient
B7=0;% coefficient
b1=0;% coefficient
B1=0;% coefficient
b2=0;% coefficient
B2=0;% coefficient
b3=0;% coefficient
B3=0;% coefficient
b4=0;% coefficient
B4=0;% coefficient
b5=0;% coefficient
B5=0;% coefficient
b6=0;% coefficient
B6=0;% coefficient
b7=0;% coefficient
B7=0;% coefficient
in this step. i want to calculate the values of my function and store them in a Matrix.
x=0:0.1:T; % x is the time axis
k=1:T/0.1+1;
funcation=x;
F(k)=funcation;
for x=0:0.1:T;
k=1:T/0.1+1;
a0=F(k)+a0;
end
A0=0.1*a0/T;
for x=0:0.1:T;
k=1:T/0.1+1;
b1=F(k).*sin(1.*wo.*x)+b1;
b2=F(k).*sin(2.*wo.*x)+b2;
b3=F(k).*sin(3.*wo.*x)+b3;
b4=F(k).*sin(4.*wo.*x)+b4;
b5=F(k).*sin(5.*wo.*x)+b5;
b6=F(k).*sin(6.*wo.*x)+b6;
b7=F(k).*sin(7.*wo.*x)+b7;
end
for x=0:0.1:T;
B1=0.1.*2.*b1./T.*sin(1.*wo.*x);
B2=0.1.*2.*b2./T.*sin(2.*wo.*x);
B3=0.1.*2.*b3./T.*sin(3.*wo.*x);
B4=0.1.*2.*b4./T.*sin(4.*wo.*x);
B5=0.1.*2.*b5./T.*sin(5.*wo.*x);
B6=0.1.*2.*b6./T.*sin(6.*wo.*x);
B7=0.1.*2.*b7./T.*sin(7.*wo.*x);
end
bq=B1+B2+B3+B4+B5+B6+B7;
for x=0:0.1:T;
k=1:T/0.1+1;
a1=F(k).*cos(1.*wo.*x)+a1;
a2=F(k).*cos(2.*wo.*x)+a2;
a3=F(k).*cos(3.*wo.*x)+a3;
a4=F(k).*cos(4.*wo.*x)+a4;
a5=F(k).*cos(5.*wo.*x)+a5;
a6=F(k).*cos(6.*wo.*x)+a6;
a7=F(k).*cos(7.*wo.*x)+a7;
end
for x=0:0.1:T;
A1=0.1.*2.*a1./T.*cos(1.*wo.*x);
A2=0.1.*2.*a2./T.*cos(2.*wo.*x);
A3=0.1.*2.*a3./T.*cos(3.*wo.*x);
A4=0.1.*2.*a4./T.*cos(4.*wo.*x);
A5=0.1.*2.*a5./T.*cos(5.*wo.*x);
A6=0.1.*2.*a6./T.*cos(6.*wo.*x);
A7=0.1.*2.*a7./T.*cos(7.*wo.*x);
end
aq=A1+A2+A3+A4+A5+A6+A7;
FQ=bq+aq+A0;
plot(F(k))
figure
plot(FQ)
The problem is in defining for loops in your script.
You shouldn't insert a semicolon ; at the end of for expression.
Instead of:
for x=0:0.1:T;
you should omit ; and write it like:
for x=0:0.1:T
then the plots work fine and both plots will be identical. As your function here is y=x, it would be a 45 degree line.
let us consider following Page :
http://djj.ee.ntu.edu.tw/S_Transform.pdf
paragraph 2.3 The Discrete S Transform
let say that we have sampled version of signal x, and given sampling frequency fs,i have calculated discrete Fourier transform using following code
function y=DFT(x);
N=length(x);
D=zeros(N,N);
for k=1:N
for n=1: N
D(k,n)=exp((-j*(k-1)*2*pi*(n-1))/N);
end
end
y=D*x'/N;
end
and started to estimate discrete S transform
function [S]=discrete_s_transform(x,fs);
%compute discrete s transform
%fs-sampling frequency
N=length(x); % length of signal
T=1/fs; % sampling period
Y=DFT(X);
how can i continue related to this part ?
clearly loops are not problem to implement,just they go from 1 to N instead of 0 to N-1 because of matlab vectors are 1 based,but what about main code?multiplication to exponential? coudl you please help me to finish S transform ?
Your sum depends only on m so I assume that other parameters as well as function H((m+n)/(NT)) are defined. Via for-loops the simplest one is:
function [S]=discrete_s_transform(x);
N=length(x); % length of signal
S=0;
m=0;
for i=1:N
S=S+H((m+n)/(NT))*exp(a)*exp(b*m/N);
m=m+1;
end
Hope that helps!
I've read some explanations of how autocorrelation can be more efficiently calculated using the fft of a signal, multiplying the real part by the complex conjugate (Fourier domain), then using the inverse fft, but I'm having trouble realizing this in Matlab because at a detailed level.
Just like you stated, take the fft and multiply pointwise by its complex conjugate, then use the inverse fft (or in the case of cross-correlation of two signals: Corr(x,y) <=> FFT(x)FFT(y)*)
x = rand(100,1);
len = length(x);
%# autocorrelation
nfft = 2^nextpow2(2*len-1);
r = ifft( fft(x,nfft) .* conj(fft(x,nfft)) );
%# rearrange and keep values corresponding to lags: -(len-1):+(len-1)
r = [r(end-len+2:end) ; r(1:len)];
%# compare with MATLAB's XCORR output
all( (xcorr(x)-r) < 1e-10 )
In fact, if you look at the code of xcorr.m, that's exactly what it's doing (only it has to deal with all the cases of padding, normalizing, vector/matrix input, etc...)
By the Wiener–Khinchin theorem, the power-spectral density (PSD) of a function is the Fourier transform of the autocorrelation. For deterministic signals, the PSD is simply the magnitude-squared of the Fourier transform. See also the convolution theorem.
When it comes to discrete Fourier transforms (i.e. using FFTs), you actually get the cyclic autocorrelation. In order to get proper (linear) autocorrelation, you must zero-pad the original data to twice its original length before taking the Fourier transform. So something like:
x = [ ... ];
x_pad = [x zeros(size(x))];
X = fft(x_pad);
X_psd = abs(X).^2;
r_xx = ifft(X_psd);