I am using the following MATLAB code to perform a Fourier transformation of a normal density function:
N=100;
j=0:(N-1);
a=-5;
b=5;
dx = (b-a)/N;
x = a+j*dx;
dt = 2*pi/(N*dx);
f1 = -N/2*dt;
f2 = N/2*dt;
t= f1+ j*dt;
GX = normpdf(x,0,1);
fft_GX = real(fft(GX))';
However, I do not get the expected bell shaped curve when I try to plot fft_GX.
The Fourier transformation of a normal density has the form of e^(-t^2/2). Can someone please help as to what I am doing incorrect?
Trying using abs instead of real.
Another helpful function to recenter the frequency domain is fftshift. Otherwise you will see the plot from 0 to 2*pi I believe, instead of the more recognizable view from -pi to pi.
fft_GX = abs(fftshift((fft(GX))');
plot(fft_GX);
You may need to do some further normalization based on the number of samples you have, but it looks more like the expected bell curve than what you were seeing originally.
Related
I want to create a 2d plot of hilbert spectrum. What i want is a time vs frequency plot, where the amplitude of the signal is represented by color changes in the plot.
What i have done is this, but I need it to look like this.
Thank you in advance
EDIT:
I have the values of amplitude and instantaneous frequency over time, that i have produced using Hilbert Huang Transform.
Let's say we only use the first IMF, and we have 100 samples. Then what we have is:
instantaneous_frequency = [f1 f2 f3 ... f100]
instantaneous_amplitude = [a1 a2 a3 ... a100]
time = [t1 t2 t3 ... t100]
What i need is a way to plot them like the second image, without using the spectrogram function, since it uses STFT and I have already applied HHT.
The code for producing the first plot is:
Time_Window = 1:101;
signal= rand(1,101);
hilb = hilbert(signal);
inst_amp = abs(hilb);
inst_th = angle(hilb);
inst_freq = diff(a_inst_th)/(1/256)/(2*pi); %instantaneous frequency
%inst_freq = remove_outliers(inst_freq,Time_Window(1:end-1));
inst_freq(end+1) = inst_freq(end); % This is done due to diff()
plot(Time_Window*4,inst_freq,'k.','MarkerSize',5)
There is already a MATLAB function to produce a spectrogram, which looks a lot like what you want. See if that solves your problem.
Consider the following example Simulink (Download example) system:
Input is a magnitude and an ever increasing angle, which will return two sine, the real and imaginary part as expected:
Calculating the Magntitude from real and imaginary part is no issue. Getting the angle in the domain between -pi and pi neither:
But I'm really struggling in calculating the original angle from the imaginary and real part. Do you have any ideas how to get rid of the discontinuity (yellow line, last picture)?
How about this example?
function [ub_ang,ang] = phase_unwrap(re, im, theta)
%#codegen
ang = atan2(im,re);
tmp = [theta ang];
uang = unwrap(tmp);
ub_ang = uang(2);
Scope1 plot is below
Scope plot is below.
Daniel's Solution was exact what I was looking for, but in case the DSP System Toolbox is not available, like in case of my project partners, I came up with the following solution:
with
function [y, corr] = phase_unwrap(zz,z,v)
%#codegen
d = diff([v,z,zz]);
if abs(d(1)) > pi
y = sign(d(1));
corr = -3/2*d(2);
else
y = 0;
corr = -3/2*d(1);
end
end
The discrete time integrator has the same sampling time as the zero-order-hold and a gain of 2*pi.
The example output is satisfying, but I still need to test it for the real case.
Use the block called unwrap. In case the toolbox is not available, this discrete implementation in simulink could be used:
I want to write a bimodal Probability Density Function (PDF with multiple peaks, Galtung S) without using the pdf function from statistics toolbox. Here is my code:
x = 0:0.01:5;
d = [0.5;2.5];
a = [12;14]; % scale parameter
y = 2*a(1).*(x-d(1)).*exp(-a(1).*(x-d(1)).^2) + ...
2*a(2).*(x-d(2)).*exp(-a(2).*(x-d(2)).^2);
plot(x,y)
Here's the curve.
plot(x,y)
I would like to change the mathematical formula to to get rid of the dips in the curve that appear at approx. 0<x<.5 and 2<x<2.5.
Is there a way to implement x>d(1) and x>d(2) in line 4 of the code to avoid y < 0? I would not want to solve this with a loop because I need to convert the formula to CDF later on.
If you want to plot only for x>max(d1,d2), you can use logical indexing:
plot(x(x>max(d)),y(x>max(d)))
If you to plot for all x but plot max(y,0), you just can write so:
plot(x,max(y,0))
I am very new to Matlab and am trying to implement the following fresnel diffraction using the fourier transform:
This is taken from the following wikipedia page: http://en.wikipedia.org/wiki/Fresnel_diffraction
I am trying a square aperture of width 5 cm.
clc;clear;
lambda=1*10^-6;
w=.05;
z=2.0;
k=(2*pi)/lambda;
x1=linspace(-0.2,0.2,2048);
y1=linspace(-0.2,0.2,2048);
U1=((abs(x1)<=(w)/2))&((abs(y1)<=(w)/2));
u1=double(U1);
figure(1)
plot(x1,u1)
g=u1'*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
G=fftshift(fft2(g));
h=(exp(1i*k*z)/(1i*lambda*z))*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
H=fftshift(fft2(h));
u2=H*G;
figure(2)
plot(x1,abs(u2));
When I plot the field u2, for any of the distances, z, that I try, it just shows up like some random pattern and not the expected diffraction pattern for a square aperture.
Like I said, I am very new to MATLAB and find it difficult to understand. I think I am making this more complicated than it needs to be, and am implementing the integrals incorrectly.
Any pointers or advice? I am quite stuck...
Thanks!
Looks like you want to simulate Fresnel diffraction with this code.
With a few changes you can view the actual image. Shown here is the amplitude. The intensity would be u2.*conj(u2).
Note however that critical sampling will occur much further away than 2 meters, more like 100 meters, for the physical parameters you are using.
Also there needs to be some additional re-scaling on the output plane, not done here. Otherwise the diffraction pattern will get very small for low Fresnel numbers.
clc;clear;
N=1048;
L=0.4;
lambda=1*10^-6;
w=.05;
z=500.0;
k=(2*pi)/lambda;
src_delta = L/N;
critical_sampling_z = N*src_delta^2/lambda;
[x1 y1] = meshgrid((-N/2 : N/2-1) * src_delta);
X = double(abs(x1)<w/2); Y = double(abs(y1)<w/2);
u1 = X.*Y;
figure(1);
colormap(gray); imagesc(mat2gray(u1));
g=u1'*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
G=fftshift(fft2(g));
h=(exp(1i*k*z)/(1i*lambda*z))*exp(1i*(pi/(lambda*z))*(x1.^2+y1.^2));
H=fftshift(fft2(h));
u2=H*G;
figure(2);
colormap(gray); imagesc(mat2gray(abs(u2)));
So I have a Matlab function that creates a series of square impulses, then I apply a noise over them and a filter, the problem is I need to change the form of the impulses into triangular form:
x = zeros(1,1000)
x(100:200) = 1
x(400:500) = 1
x(700:800) = 1
plot(x)
Try to define a function that creates a triangle wave and then use it (or define it by hand everytime). Something like this should work fine:
x = zeros(1,1000);
tri = #(x) [(0:(floor(x/2)-1))/floor(x/2),1,((floor(x/2)-1):-1:0)/floor(x/2)];
x(100:200) = tri(101);
x(400:500) = tri(101);
x(700:800) = tri(101);
plot(x)
If this is not what you was asking for, eg if you meant that you want a sawtooth wave, then you should check out the sawtooth function, try:
x = 0:0.1:15;
y=sawtooth(x,0.5);
plot(x,y);
However, I would encourage you to change the name of the question, which is really about what to plot and not which plot function you are supposed to use.