Audio Signal eval() Error (for Fourier Series Approximation)

Hello everyone, I am trying to calculate the fourier series of my voice but I cannot do it. My code works for square wave, sine wave everything else but for audio file it gives error. What is my mistake and how can I converge by different methods. Thanks.
My code is:
recorder= audiorecorder(16000,8,1);
disp('please record your voice');
drawnow();
pause(1);
recordblocking(recorder, 2);
play(recorder);
data=getaudiodata(recorder);
subplot(4,1,1);
plot(data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms n aSym(n) bSym(n) x(t) t A(t)
To=2;
f=1/To;
wo=2*pi*f;
a=trapz(data);
a0= 4.08*a;
aSym(n)=simplify(int(data*cos(n.*wo.*t),t,0,To)/(To/2))
bSym(n)=simplify(int(data*sin(n.*wo.*t),t,0,To)/(To/2))
nMax = 9;
n = 1:nMax;
an = eval(aSym(n));
bn = eval(bSym(n));
converge = a0;
for m = 1:nMax
converge = converge + an(m)*cos(m*wo*t) + bn(m)*sin(m*wo*t);
end
t = 0:0.0001:2;
subplot(4,1,2);
last = eval(converge);
plot(t,last, 'linewidth', 2)
Error using eval
Argument must contain a character vector.
Error in Untitled2 (line 25)
an = eval(aSym(n));

8 commentaires

hello
I doubt that matlab can solve your symbolic equation for data measured. It works if data has an analytical solution
you have to come back to a purely numerical DFT method
what should i do with fft? I get some plots like e^-at and what those values mean sir?
Fourier Series for periodic signals .
You should apply Fourier Transform to your Audio signal , you will use fft to compute the DFT .
okey i take fft and i get too much values, what should i do next sir? :)
you should never eval() a symbolic expression. Symbolic expressions display in a language that is not MATLAB and which is not mupad (the symbolic engine).
If you have symbolic variables in an expression that you want to replace with numeric values then use subs(). If you have a symbolic expression that you want to evaluate numerically if feasible then you can use vpa()
sir what should i do to calculate for example an, i have an arbitary signal im facing with problems.
Unfortunately for enrique128gok Google has a cache:
Audio Signal eval() Error (for Fourier Series Approximation)
Hello everyone, I am trying to calculate the fourier series of my voice but I cannot do it. My code works for square wave, sine wave everything else but for audio file it gives error. What is my mistake and how can I converge by different methods. Thanks.
My code is:
recorder= audiorecorder(16000,8,1);
disp('please record your voice');
drawnow();
pause(1);
recordblocking(recorder, 2);
play(recorder);
data=getaudiodata(recorder);
subplot(4,1,1);
plot(data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
syms n aSym(n) bSym(n) x(t) t A(t)
To=2;
f=1/To;
wo=2*pi*f;
a=trapz(data);
a0= 4.08*a;
aSym(n)=simplify(int(data*cos(n.*wo.*t),t,0,To)/(To/2))
bSym(n)=simplify(int(data*sin(n.*wo.*t),t,0,To)/(To/2))
nMax = 9;
n = 1:nMax;
an = eval(aSym(n));
bn = eval(bSym(n));
converge = a0;
for m = 1:nMax
converge = converge + an(m)*cos(m*wo*t) + bn(m)*sin(m*wo*t);
end
t = 0:0.0001:2;
subplot(4,1,2);
last = eval(converge);
plot(t,last, 'linewidth', 2)
Error using eval
Argument must contain a character vector.
Error in Untitled2 (line 25)
an = eval(aSym(n));
(Answers Dev) Restored edit

Connectez-vous pour commenter.

 Réponse acceptée

syms Data
assume(n, 'integer');
aSym(n) = subs( simplify(int(Data*cos(n.*wo.*t),t,0,To)/(To/2)), Data, data);
bSym(n) = subs( simplify(int(data*sin(n.*wo.*t),t,0,To)/(To/2)), Data, data);
n = 1:nMax;
an = double( aSym(n) );
bn = double( bSym(n) );
However, you will find that with that particular To that all of your a entries and all of your b entries are exactly 0.

7 commentaires

By the way: you do not have the right time range for fourier transform.
shouldn't I give any values to To sir?
For the purpose of the above code, I assumed your code had already done
To=2;
f=1/To;
wo=2*pi*f;
If you were to stay with those integrals (which I do not think are the correct ones) then you can pre-calculate
syms Data TO real
syms n integer
F = 1/TO;
WO = 2*pi*F;
Int_a = simplify(int(Data*cos(n.*WO.*t),t,0,TO)/(TO/2));
Int_b = simplify(int(Data*sin(n.*WO.*t),t,0,TO)/(TO/2));
.. and you will find that they are both exactly 0 no matter what integer n you use and no matter what TO you use.
I would remind you that Fourier Transform of continuous functions involves a single infinite integral with no summation; I would remind you that Discrete Fourier Transform for discrete data involves no integrals but does involve a summation (that would approximate an integral in the limiting case as n -> infinity)
sir i can not solve my problem but thanks for your effort, I accepted it :)
Why did you edit away your question? That is very rude.
enrique128
enrique128 le 16 Nov 2020
Modifié(e) : enrique128 le 16 Nov 2020
I am developing my code and this is my project, I asked my question and cannot find the solution. The code should be private, I can not understand why u share all of my code again sir. I already thanked sir Walter Roberson for his helps, I am trying to find new solutions. I think it is not about rudeness or something like that, this is my project and should be "private". As you can see this is the only topic that I edited, if I am a rude person I should have deleted all of my questions :)
Stephen23
Stephen23 le 16 Nov 2020
Modifié(e) : Stephen23 le 16 Nov 2020
"The code should be private"
By submitting your question on this forum you consented to the terms and conditions given here
which clearly state that you release your code under the Creative Commons Attribution Share Alike 3.0 license. So you have already given everyone on the entire planet permission to copy, distribute, and reuse your code (with appropriate attribution, of course, following the requirements of that license).
"I can not understand why u share all of my code again sir."
Because this answer does not make sense without it.
Because all of the volunteers on public internet forums volunteer their time on the understanding that their time helping one person might also be of interest and use to other people. This is the nature of such public forums.
By deleting the content of your question you treat those volunteers as your own unpaid private consultants. You unilaterally decide to reject one of the reasons for taking part in a public forum, which is that it is public.
If you want private help then you can find plenty of private consultants who will be happy to sign an NDA.

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by