Imprecise plotting with MATLAB - matlab

I need to model an electric rectifier and to plot both in and out signals with MATLAB. The rectifier is made of a RC circuit which charge as fast as the voltage increases but discharges way slower so the out signal is more or less flat. It's supposed to look like that:
.
I tried to code it on MATLAB and I got this (my circuit rectifies negative voltage but same principle):
.
To get the same figure as the one from wikipedia I tried to compute the intersection between the downing exp curve (red) and the rising sinus curve (blue) so I just had to add a sin curve and a downing exp curve at right intervals to get the out signal.
Here is my code:
f=#(x)sin(2*pi*250000*x+pi/2);%oscillateur de référence
f1=#(x)sin(2*pi*250000*x);
g=#(x)exp(-x*10^4);%décharge du détecteur de crête
h=#(x)f(x)-g(x);%intersection des deux fonctions
format long;
inter=fzero(h,[3.82*10^-6,3.90*10^-6]);
y1=g(0:10^-12:inter);
y2=f(inter:10^-12:4*10^-6);
y3=sin(2*pi*250000*(0:10^-12:1*10^-6));
y=-[y3 y1 y2 y1 y2];
y4=-f1(linspace(0,8*10^-6,length(y)));
x=linspace(0,10*10^-6,length(y));%abscisse
plot(x,y,x,y4);
But why is there a gap between the curves on my figure ?

You really don't need to find the intersection points. You can reproduce the same curve with a series of nested max() calls and logical operations. Here's an example:
f=#(x)sin(2*pi*250000*x);
discharge=1e-6; %# quarter period when discharge begins
g=#(x)exp(-rem(x-discharge,(1e-5)/2.5)*10^5); %#modulo over the period to repeat.
h=#(x)max(f(x).*(x<discharge),max(f(x),g(x)).*(x>=discharge)); %# the rectified signal
y=linspace(0,1e-5,1e4);
plot(y,f(y),y,h(y))

Related

Matlab not plotting the exact fourier signal

I'm trying to plot a simple signal in fourier domain using Matlab. It's not plotting the correct signal. Here is my code:
clc;
clear all;
close all;
x=1:0.001:10;
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);
f=f1+f2+f3;
plot(2*pi*x,fft(f1));
figure
plot(x,fft(f1));
I've expected a peak at 10 since the frequency is 10. But it is giving a peak at some other point
Here are the two plot images:
This is the image for plot(x,fft(f1))
This is the image for plot(2*pi*x,fft(f1))
It is not showing the peak at 10.I even tried using abs(fft(f1)). No luck :/
Isn't it the correct way to plot signal in fourier domain?
The fft function assumes unit time step. In order to correct for non unit time step you need to define the frequency component based on the nyquist rate. The following code plots the magnitude of the fft with the correct frequency axis.
clc;
clear all;
close all;
x=1:0.001:10;
% ^ this is your sampling time step
f1=sin(2*pi*10*x);
f2=sin(2*pi*15*x);
f3=sin(2*pi*30*x);
% bounds of fourier transform based on sampling rate
Fs = 1/0.001;
ff = linspace(-Fs/2,Fs/2,numel(x));
F1 = fftshift(fft(f1)/numel(x));
F2 = fftshift(fft(f2)/numel(x));
F3 = fftshift(fft(f3)/numel(x));
figure();
plot(ff,abs(F1),'-r'); hold on;
plot(ff,abs(F2),'-b');
plot(ff,abs(F3),'-k');
Edit: To answer OPs question in the comment.
Speaking in normalized frequency units (assuming sampling rate of 1). The fft function returns the frequency response from 0 to 2*pi radians, but due to some signal processing properties and the way that discrete signals are interpreted when performing an FFT, the signal is actually periodic so the pi to 2*pi section is identical to the -pi to 0 section. To display the plot with the DC component (0 frequency) in the center we use fftshift which does a circular shift equal to 1/2 the length of the signal on the data returned by fft. Before you take the ifft make sure you use ifftshift to put it back in the right place.
Edit2: The normalization term (/numel(x)) is necessary to estimate the continuous time fourier transform using the discrete fourier transform. I don't remember the precise mathematical reason off the top of my head but the examples in the MATLAB documentation also imply the necessity of this normalization.
Edit 3: The original link that I had is down. I may come back to add a more detailed answer but in the mean time I definitely recommend that anyone interested in understanding the relationship between the fundamentals of the FS, FT, DTFT, and DFT watch Professor Oppenheim's hilariously old, but amazingly informative and straightforward lectures on MIT OpenCourseWare.

3-D Plot in MATLAB Containing: Time, Frequency and Power Spectral Density

I am currently working on a project for my Speech Processing course and have just finished making a time waveform plot as well as both wide/narrow band spectrograms for a spoken word in Spanish (aire).
The next part of the project is as follows:
Make a 3-D plot of each word signal, as a function of time, frequency and power spectral density. The analysis time step should be 20ms, and power density should be computed using a 75%-overlapped Hamming window and the FFT. Choose a viewing angle that best highlights the signal features as they change in time and frequency.
I was hoping that someone can offer me some guidance as to how to begin doing this part. I have started by looking here under the Spectrogram and Instantaneous Frequency heading but was unsure of how to add PSD to the script.
Thanks
I am going to give you an example.
I am going to generate a linear chirp signal.
Fs = 1000;
t = 0:1/Fs:2;
y = chirp(t,100,2,300,'linear');
And then, I am going to define number of fft and hamming window.
nfft=128;
win=hamming(nfft);
And then I am going to define length of overlap, 75% of nfft.
nOvl=nfft*0.75;
And then, I am performing STFT by using spectrogram function.
[s,f,t,pxx] = spectrogram(y,win,nOvl,nfft,Fs,'psd');
'y' is time signal, 'win' is defined hamming window, 'nOvl' is number of overlap, 'nfft' is number of fft, 'Fs' is sampling frequency, and 'psd' makes the result,pxx, as power spectral density.
Finally, I am going to plot the 'pxx' by using waterfall graph.
waterfall(f,t,pxx')
xlabel('frequency(Hz)')
ylabel('time(sec)')
zlabel('PSD')
The length of FFT, corresponding to 20ms, depends on sampling frequency of your signal.
EDIT : In plotting waterfall graph, I transposed pxx to change t and f axis.

MATLAB fft y axis meaning

I have a time varying signal (a), which I take the fft of. I need to multiply a frequency dependent weighting factor by the fft's y axis value; however if I do:
xdft = fft(a);
xdft = xdft(1:length(x)/2+1); % only retaining the positive frequencies
freq = Fs*(0:(L/2))/L;
And plot(freq,xdft) I get a peak fft value (y axis)of ~2000 at the correct frequency of the signal. But original signal peak value (amplitude) was ~46. I need to know how the numbers relate so I can weight the fft values.
You forgot to divide by the DFT length. Take a look at this example.
Consider that the output of fft is complex. So if you want to plot the real power spectral density you should multiply the output by its complex conjugate like this:
Pyy = xdft.*conj(xdft)/L;
Edit:
For the amplitude spectrum you should do something like this:
xdft=abs(xdft/L); % same as sqrt(xdft.*conj(xdft))/L
Y=xdft(1:L/2+1); % copy half of data since the other half is redundant
Y(2:end-1) = 2*Y(2:end-1); % correct the amplitudes
Edit 2:
Just wanted to point to a really great book (the best in my opinion) which explains how fourier series work (and much more) in a really easy and understandable way.
Alan V. Oppenheim, Alan S. Willsky, and S. Hamid Nawab. 1996. Signals & Systems (2nd Ed.). Prentice-Hall, Inc., Upper Saddle River, NJ, USA.

Finite Difference Time Domain (FTDT) method for 1D EM Wave

I have attempted to write a code in order to solve the following coupled partial differential EM wave equations:
The code employs finite difference time domain using the Yee algorithm which can be read about in the following two online documents:
http://www.eecs.wsu.edu/~schneidj/ufdtd/ufdtd.pdf
http://www.eecs.wsu.edu/~schneidj/ufdtd/chap3.pdf
I want my source at the left hand side boundary to be a sinusoidal wave with parameters as such:
Ex(0,t) = E0 sin(2πft) for 0 ≤ t ≤ 1/f
The code updates the electric and magnetic field properties of the wave with each loop.
My initial code is as follows:
%FDTD Yee algorithm to solve coupled EM wave equations
clear
clc
G=50; %Specify size of the grid
f=10^3; %choose initial frequency of wave in hertz
e=1; %specify permitivity and permeability (normalised condition)
u=1;
Nt=150; %time steps
E0=1; %Electric Field initial amplitude
%Specify step sizes using corruant condition
c=3*10^8;
dx=0.01;
dt=dx/2*c;
%make constant terms
c1=-dt/(dx*e);
c2=-dt/(dx*u);
%create vgector place holders
Ex=zeros(1,G);
Hy=zeros(1,G);
%create updating loop
M=moviein(Nt);
for t=1:Nt
% Spatial Ex
for k=2:G-1
Ex(k)=Ex(k)+c1*(Hy(k)-Hy(k-1));
end
Ex(G)=0; %PEC boundary condition
%E Source at LHS boundary
Ex(1)=E0*sin(2*pi*f*t);
%Spatial Hy
for n=1:G-1
Hy(n)=Hy(n)+c2*(Ex(n)-Ex(n+1));
end
Hy(G)=0; %PMC boundary condition
plot(Ex);
M(:,t) = getframe;
end
movie(M,1);
Basically I want to create an updating movie which shows the sinusoidal wave propagating to the right hand side boundary coded as a perfect electrical conductor; therefore reflecting the wave, and then propagating back to the left hand side boundary coded as a perfect insulator; absorbing the wave.
The problems I have are as follows:
1) I'm not sure how to properly implement my desired source. It don't appear to be purely sinusoidal.
2) The wave I've coded begins to propagate but it quickly disappears for the majority of the simulation. I do not know why this is occurring
3) I do not know much about running a movie simulation and the plot oscillates as the solution is being run. How can I stop this?
Your wave attenuates because the diference equations are not correctly implemented; instead:
Ex(k)=Ex(k)+c1*(Hy(k)-Hy(k-1));
you should use
Ex1(k)=Ex(k)+c1*(Hy(k)-Hy(k-1));
and instead of:
Hy(n)=Hy(n)+c2*(Ex(n)-Ex(n+1));
you should use:
Hy1(n)=Hy(n)+c2*(Ex(n)-Ex(n+1));
and, in the end of the loop update the current "dataframe":
Hy = Hy1;
Ey = Ey1;
(you should take care also the boundary conditions).
If you want a fixed plot frame that doesn't change when your data changes, create first a axis where you can plot into, with a defined xmin/max and ymin/max, see http://www.mathworks.com/help/matlab/ref/axis.html
You should set the Courant number closer to 1 say 0.995. Thus delta_t = 0.995*delta_x/c.
Also assuming delta_x is in METRIC units then e and u should be in metric units.
I do not know about the specific coding language used but in c or c++ there is no need for intermediate variable Ey1 etc.
Also there should be at least 10 samples per wavelength for accuracy ( preferably 60). Thus wavelength = 60*delta_x and thus the frequency equals roughly of the order 10 to power of 9. Also, I think the sinesoidal source should be E0 * sin(2* pi * f * t * delta_t). You need to adjust your constants, and try it again

Units for Matlab PSD

Im new to matlab and as part of my university assignment,im supposed to draw up these signals on matlab.and i have some problems regarding the units of the psd plots.
1)j=0:1/100:1; %time index
z=sin(2*pi*5*j); %sine wave signal
z=z*2;
plot(z),xlabel('Sampling Points'),ylabe;('Amplitude');
figure,psd(z)
2)noise=rand(1,100);
plot(noise);
figure,psd(noise);
3)[B,A]=butter(10,3/50,'low');
LPFz=filtfilt(B,A,z);
plot(LPFz)
figure,psd(LPFz)
4)y=wavread('sp1.wav');
Fs=44100; %sampling frequency
wavplay(y,Fs);
save sp1.mat y
plot(y(:,1)),title('Waveform of Speech'),xlabel('Sample Points'),ylabel('Amplitude')
figure,psd(y(:,1))
i would be extremely grateful to anyone who can help my by telling me the units i should use for the x axis of the psd spectrum,y axis is in dB ,that i got.i dont know what to out for x
If you are looking at your first example the x coordinate of plot (z) should be time. After all you define j as a time index and generate your z-function to be a function z(t).
If I am not mistaken your z function is a sine of structure 2*pi*omega*time with omega beeing a constant (5).
If u have further information about that constant you can be more specific about your x-axis.
As a matter of fact you can just look at your function: exponents as well as triangular functions (sine, cosine ,etc) have to be without dimension (seconds, meters ,kg, and so on)
If you were wondering about the units for the psd function, it is a spectrum. This means the x-axis is a frequency domain(1/seconds = 1Hz). In Matlab 8.2 it is recommended to use spectrum(z) instead of psd(z)