can someone help me to plot in audio plocessing?

4 views (last 30 days)
hi fiends, i have a problem with my program, i have to process an audio and make a different transformations for this audio, the fist transformatios is a delay in the time that the audio begins, but i have a doubt because when i plot the signal, the numbers dont coincide with the number i have for the seconds that i have to delay the signal. can somebody help me? please. this is my code, i have try in two different ways and bouth of them give me the same result.
i 've try this way :
[x,fs]=wavread('anita.wav');
cor=input('seconds you wanna delay: ');
cor=cor*fs;
otra=zeros(cor,2);
onda=vertcat(otra,x);
wavwrite(onda,fs,'delay.wav');
[y,Fs]=wavread('delay.wav');
sound(x,Fs);
sound(y,Fs);
subplot(2,1,1);
plot(x);
subplot(2,1,2)
plot (y);
and this:
[x,fs]=wavread('anita.wav');
cor=10;
cor=cor*fs;
otra=zeros(cor,2);
onda=vertcat(otra,x);
wavwrite(onda,fs,'delay.wav');
[y,Fs]=wavread('delay.wav');
%sound(x,Fs);
%sound(y,Fs);
j=length(y);
f=y((end-j+1):1:end)
subplot(2,1,1);
plot(x)
subplot(2,1,2)
plot (f)
the other transformation is an expansion an a compression with the audio, the faster an the lower you want to make the audio, but again i think that the plots are wrong, because the numbers don't coincide with the time. can someone help me to make them right? this is the code:
compression
[x,Fs]=wavread('anita.wav');
t=input('seconds to compress:');
rut=Fs*t;
f=z(1:rut);
plot(x);
plot(f);
expansion
[x,Fs]=wavread('anita.wav');
t=input('seconds to expand:');
rut=Fs/t;
f=z(1:rut);
plot(x);
plot(f);

Answers (1)

Geoff Hayes
Geoff Hayes on 16 Sep 2014
Alejandra - consider plotting time along the x-axis (in your plots) rather than samples. This may produce results that convince you that the delay is correct. Change the
subplot(2,1,1);
plot(x);
subplot(2,1,2);
plot (y);
to
subplot(2,1,1);
plot(linspace(0,length(x)/fs-1/fs,length(x)),x);
subplot(2,1,2)
plot(linspace(0,length(y)/Fs-1/Fs,length(y)),y);
The above uses linspace to create the times (in seconds) for each sample in x and y using fs and Fs respectively (note that the sample rate for each is the same for both data sets).
When I run the above, and choose a delay of five seconds, the results are as expected - a gap of five seconds appears in the second plot.
As for the "compression", your code is reducing the wave file to the first t seconds. Again, use subplot and change the x-axis to seconds rather than samples
subplot(2,1,1);
plot(linspace(0,length(x)/Fs-1/Fs,length(x)),x);
h = subplot(2,1,2)
plot(linspace(0,length(f)/Fs-1/Fs,length(f)),f);
xlim(h,[0 length(x)/Fs-1/Fs]);
Note that the x-axis limits for the second sub-plot are explicitly set to that which the first plot is using so that we can see the effect of the reduction.
Also note that the line of code
f=z(1:rut);
must be changed to
f=x(1:rut);
since z is not a local variable.
Though I wonder if this is what you have really intended. Could you elaborate on what you mean by ...other transformation is an expansion an a compression with the audio, the faster an the lower you want to make the audio... Do you really mean to change the sample rate so that the audio plays at a faster rate or a lower rate?
  4 Comments
alejandra
alejandra on 16 Sep 2014
yes thats what i want to see in the plot, the real compression of the audio, i mean when i put in the time 2, the graphic should reduce in 2 units,thats why the plot should reduce to 2.5 obviously when my length of the audio has 5 seconds, when i put 3, the graphic should be reduced to 1.75... and in the expansion should do the same thing, if i put 2 (in the case of 5 seconds) the graphic have to has 10 units. and so...btw, thank you so much so much for the help, i appreciate alot!!!!! :D
Geoff Hayes
Geoff Hayes on 17 Sep 2014
As a test wave file, I am using the handel.mat data that comes with MATLAB
load handel.mat
hfile = 'handel.wav';
wavwrite(y, Fs, hfile)
clear y Fs file
handel.wav is roughly nine seconds in length.
If we wish to "compress" the audio data by a factor of two, then we can calculate the new sampling rate as twice that of the original sampling rate
[x,xFs] = wavread('handel.wav');
compFactor = 2;
yFs = xFs*compFactor; % calculate the new sampling rate
and plot the data as
figure
u = subplot(2,1,1);
plot(u,linspace(0,length(x)/xFs-1/xFs,length(x)),x);
title(u,['Original Audio Data with sampling rate ' num2str(xFs) ' Hz']);
xlabel(u,'Seconds');
v = subplot(2,1,2);
plot(v,linspace(0,length(x)/yFs-1/yFs,length(x)),x);
title(v,['Compressed Audio Data with sampling rate ' num2str(yFs) ' Hz']);
xlim(v,get(u,'XLim'));
xlabel(v,'Seconds');
Viewing the plots, you will observe that the length of the compressed audio data half that of the original (roughly 4.5 seconds).
If we wish to "expand" the audio data by a factor of two, then we can calculate the new sampling rate as half that of the original sampling rate
expFactor = 2;
yFs = xFs/expFactor;
wavwrite(x,yFs,'expdHandel.wav');
[y,yFs] = wavread('expdHandel.wav');
figure
u = subplot(2,1,1);
plot(u,linspace(0,length(x)/xFs-1/xFs,length(x)),x);
title(u,['Original Audio Data with sampling rate ' num2str(xFs) ' Hz']);
xlabel(u,'Seconds');
v = subplot(2,1,2);
plot(v,linspace(0,length(x)/yFs-1/yFs,length(x)),x);
title(v,['Expanded Audio Data with sampling rate ' num2str(yFs) ' Hz']);
xlim(u,get(v,'XLim'));
xlabel(v,'Seconds');
Viewing the plots, you will observe that the length of the expanded audio data is twice that of the original (roughly 19 seconds).
Note that if you wish to hear the compressed or expanded audio data, just do
wavwrite(x,yFs,'newAudio.wav');
[y,yFs] = wavread('newAudio.wav');
sound(y,yFs);
I hope that this helps!

Sign in to comment.

Categories

Find more on Time-Frequency Analysis in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!