i have vin which is the input voltage of circuit .
(𝜏=taw)
vin[(-taw/4)-->(taw/4)]=0.75
vin[(taw/4)-->(3*taw/4)]=-0.25
and the voltage is repeated,this is a square input voltage.
𝑉𝑖𝑛(𝑡) = ∑𝑎𝑘 cos (2𝜋𝑘t/𝜏)
now i need to find Fourier coefficients:
ak= 2/𝜏*∫ 𝑉𝑖𝑛(𝑡) cos (2𝜋𝑘𝑡/𝜏) 𝑑𝑡 k>0.
ak=1/𝜏∫ 𝑉𝑖𝑛(𝑡)𝑑t k<0.
so basicly i need to write a function with the input n, and the function finds fourier coefficients, and returns a vector with (n+1) first fourier coefficients
now my problem is i how can i right vin in the integral ?
i wrote t-nhis and i know it is wrong i will aprciate a little help
[coefs]=a_n_coefs(n)
syms t k
taw=(i still don't know what to do here but i know that it have no effect)
coefs= 1:(n+1);
a0=2*int(0.75,t,0,taw/4)+2*int(-0.25,t,0,taw/4);
end
obviuosly thats nothing but i basicly want to know how i can write the integral then i will continue and write the function.
Related
I'm writing a function that calculates the Taylor series of any Function.
syms x
y=cos(x);
y0=0;
a=0;
for i=0:25
diff(y,i); %%Gives the derivative formula
y0=y0+diff(y,i)*((x-a)^i)/factorial(i); %%sums every new element of the series
end
x=0:0.1:2*pi;
res = subs(y0,x);
plot(x,res,x,cos(x))
This is the Matlab code.
My problem is that it graphs cos(2x) instead of cos(x), similarly it graphs ln(2x) instead of ln(x) and so on.
I have checked the factorials and they appear to be correct.
What could be the problem, have I messed up the series or have I made a Matlab mistake?
You are constructing the Taylor polynomial around the point x with incrementx-a, that is, you are computing an approximation for
f(x+(x-a))=f(2*x-a)
Now as a=0, this means that as observed, you get f(2*x).
You would need to evaluate the derivatives at a to get the correct coefficients.
y0=y0+subs(diff(y,i),a)*((x-a)^i)/factorial(i); %%sums every new element of the series
As part of a course in signal processing at university, we have been asked to write an algorithm in Matlab to calculate the single sided spectrum of our signal using DFT, without using the fft() function built in to matlab. this isn't an assessed part of the course, I'm just interested in getting this "right" for myself. I am currently using the 2018b version of Matlab, should anyone find this useful.
I have built a signal of a 1 KHz and 2KHz sinusoid, phase shifted by 135 degrees (2*pi/3 rad).
then using the equations in 9.1 of Discrete time signal processing (Allan V. Oppenheim) and Euler's formula to simplify the exponent, I produce this code:
%%DFT(currently buggy)
n=0;m=0;
for m=1:DFT_N-1 %DFT_Fmin;DFT_Fmax; %scrolls through DFT m values (K in text.)
for n=1:DFT_N-1;%;(DFT_N-1);%<<redundant code? from Oppenheim eqn. 9.1 % eulers identity, K=m and n=n
X(m)=x(n)*(cos((2*pi*n*m)/DFT_N)-j*sin((2*pi*n*m)/DFT_N));
n=n+1;
end
%m=m+1; %redundant code?
end
This takes x as the input, in this case the signal mentioned earlier, as well as the resolution of the transform, as represented by the DFT_N, which has been initialized to 100. The output of this function, X, should be something in the frequency domain, but plotting X yields a circular plot slightly larger than the unit circle, and with a gap on the left hand edge.
I am struggling to see how I am supposed to convert this to the stem() plots as given by the in-built DFT algorithm.
Many thanks, J.
This is your bug:
replace X(m)=x(n)*(cos.. with X(m)=X(m)+x(n)*(cos..
For a given m, it does not integrate over the variable n, but overwrites X(m) only the last calculation for n = DFT_N-1.
Notice that integrating over n=1:DFT_N-1 omits one harmonic, i.e., the first one, exp(-j*2*pi). Replace
n=1:DFT_N-1 with n=1:DFT_N to include that. I would also replace m=1:DFT_N-1 with m=1:DFT_N for plotting concerns.
Also replace any 2*pi*n*m with 2*pi*(n-1)*(m-1) to get the phase right, since the first entry of X should correspond to zero-frequency, yielding sum_n x(n) * (cos(0) + j sin(0)) = sum_n x(n). If your signal x is real-valued then the zero-frequency component X(1) should be real-valued, angle(X(1))=0.
Last remark, don't forget to shift zero-frequency component to the center of the spectrum for better visibility, X = circshift(X,floor(size(X)/2));
If you are interested in the single-sided spectrum only, than you can just calculate X(m) for m=1:DFT_N/2 since X it is conjugate symmetric around m=DFT_N/2, i.e., X(DFT_N/2+m) = X(DFT_N/2-m)', due to exp(-j*(pi*n+2*pi/DFT_N*m)) = exp(-j*(pi*n-2*pi/DFT_N*m))'.
As a side note, for a given m this program calculates an inner product between the array x and another array of complex exponentials, i.e., exp(-j*2*pi/DFT_N*m*n), for n = 0,1,...,N-1. MATLAB syntax is very convenient for such calculations, and you can avoid this inner loop by the following command
exp(-j*2*pi/DFT_N*m*(0:DFT_N-1)) * x
where x is a column vector. Similarly, you can avoid the first loop too by expanding your complex exponential vector row-wise for every m, i.e., build the matrix exp(-j*2*pi/DFT_N*(0:DFT_N-1)'*(0:DFT_N-1)). Then your DFT is simply
X = exp(-j*2*pi/DFT_N*(0:DFT_N-1)'*(0:DFT_N-1)) * x
For single-sided spectrum, instead use
X = exp(-j*2*pi/DFT_N*(0:floor((DFT_N-1)/2))'*(0:DFT_N-1)) * x
MATLAB failed to compute the fft of this function:
syms t;
x= 1/(1+t^2);
X= fft(x)
And threw this error:
Undefined function 'fft' for input arguments of type 'sym'.
Why didn't it take the Fourier transform of the symbolic variable? I think it should because we may want to obtain the result as a symbolic value.
I also tried to solve the problem with non-symbolic variable.
t= -10:0.01:10;
x= zeros(2001);
x= 1/(1+t.^2);
fft(x)
this time my error is:
Matrix dimensions must agree.
However, they have the same dimensions.
Where is my fault?
Source: http://www.mathworks.com/matlabcentral/newsreader/view_thread/315950
FFT is a method for numerical discrete Fourier Transform (DFT).
Basically, what you're asking doesn't make sense. FFT is designed to work numerically on discrete data (a sequence of numbers).
What you want is the Fourier Transform of your symbolic expression. For that, I believe you want fourier.
The documantation on fft says:
Y = fft(X) computes the discrete Fourier transform (DFT) of X using a fast Fourier transform (FFT) algorithm.
Symbolic functions are continuous, not discrete. Hence, the algorithm fails.
With regards to your second question: use element-wise operators, by adding a dot:
t= -10:0.01:10;
x= zeros(2001);
x= 1./(1+t.^2);
fft(x)
MATLAB complains about matrix dimensions, because you are trying to divide a scalar (i.e. a 1 x 1 matrix) by a vector of length 2001. element-wise division solves that problem.
I need help in implementing a MATLAB code to compute the frequency of the Fourier coefficients for 2D data. I first applied MATLAB's fft2 on the data followed by the fftshift, all I need to do now is compute the f=sqrt(fx*fx+fy*fy) such that fx is the coefficients along the columns and fy is the coefficients along the rows.
data=data-mean(data(:))
dft=fft2(data,1024,1024);
dftshift=fftshift(dft);
wn=2*pi/1024
cx=floor(1024/2)+1
for I=1:1024
for J=1:1024
freqx=(I-cx)*wn;
freqy=(J-cy)*wn;
freq=sqrt(freqx*freqx+freqy*freqy);
end
end
I am not doing signal processing. But in my area, I will use the spectral density of a matrix of data. I get quite confused at a very detailed level.
%matrix H is given.
corr=xcorr2(H); %get the correlation
spec=fft2(corr); % Wiener-Khinchin Theorem
In matlab, xcorr2 will calculate the correlation function of this matrix. The lag will range from -N+1 to N-1. So if size of matrix H is N by N, then size of corr will be 2N-1 by 2N-1. For discretized data, I should use corr or half of corr?
Another problem is I think Wiener-Khinchin Theorem is basically for continuous function. I have always thought that Discretized FT is an approximation to Continuous FT, or you can say it is a tool to calculate Continuous FT. If you use matlab build in function 'fft', you should divide the final result by \delta x.
Any kind soul who knows this area well there to share some matlab code with me?
Basically, approximating a continuous FT by a Discretized FT is the same as approximating an integral by a finite sum.
We will first discuss the 1D case, then we'll discuss the 2D case.
Let's look at the Wiener-Kinchin theorem (for example here).
It states that :
"For the discrete-time case, the power spectral density of the function with discrete values x[n], is :
where
Is the autocorrelation function of x[n]."
1) You can see already that the sum is taken from -infty to +infty in the calculation of S(f)
2) Now considering the Matlab fft - You can see (command 'edit fft' in Matlab), that it is defined as :
X(k) = sum_{n=1}^N x(n)*exp(-j*2*pi*(k-1)*(n-1)/N), 1 <= k <= N.
which is exactly what you want to be done in order to calculate the power spectral density for a frequency f.
Note that, for continuous functions, S(f) will be a continuous function. For Discretized function, S(f) will be discrete.
Now that we know all that, it can easily be extended to the 2D case. Indeed, the structure of fft2 matches the structure of the right hand side of the Wiener-Kinchin Theorem for the 2D case.
Though, it will be necessary to divide your result by NxM, where N is the number of sample points in x and M is the number of sample points in y.