Thursday, November 25, 2010

Measurement of speed of sound OR measurement of distance using sound

This is set up as a lab experiment demonstrating the use of cross correlation. The PC sound card is used together with wavplay  and  wavrecord in MATLAB to generate and measure, using two microphones a fixed distance apart, a noise burst. Wavplay is used in asynchronous mode to play the noise an immediately return control to the command window so that the sound can be recoded using  wavrecord.

Fs=44100;
T=1/Fs;
s=.1;
N=s*Fs;
x=idinput(N);%,'prbs');
xx=zeros(N,2);xx(:,2)=x;
t=0:1/Fs:(N-1)/Fs;
wavplay(xx,Fs,'async')
y=wavrecord(s*Fs,Fs,2);
figure(1)
subplot(2,1,1)
plot(t,y);%plot(t,y(:,1))
subplot(2,1,2)
plot(t,y(:,2))
figure(2)
%subplot(2,1,2)
rxx=xcorr(y(:,1),y(:,2),200);
l=-200:200;
plot(l,rxx,'r');grid on
[A,I]=max(rxx);
Tlag=T*(200-I);
v=340;
d=v*Tlag   % Display distance in command window

Wednesday, February 24, 2010

Visualising the Complex Exponential

 
x=-20:.1:20;y=-20:.1:20;
[X,Y] = meshgrid(x,y);
a=-0.05;b=0.75;
% Real Component :    e^(ax)cos(by)
z=exp(a*X).*cos(b*Y);
subplot(2,2,1)
mesh(X,Y,z)
zrr=exp(a*x).*cos(b*0);
hold on
plot3(x,zeros(length(x)),zrr,'r','LineWidth',3)
zir=exp(a*0).*cos(b*y);%zi=exp(j*b*y);
plot3(zeros(length(x)),y,zir,'r','LineWidth',4)
zrr=exp(a*x).*cos(b*y);%zrr.*zir;
plot3(x,y,zrr,'b','LineWidth',4)
hold off

% Imaginary Component :    je^(ax)sin(by)
z=exp(a*X).*sin(b*Y);
subplot(2,2,2)
mesh(X,Y,z)
hold on
zri=exp(a*x)*sin(0);
plot3(x,zeros(length(x)),zri,'r','LineWidth',3)
zii=exp(a*0)*sin(b*y);
plot3(zeros(length(x)),y,zii,'r','LineWidth',4)
zii=exp(a*x).*sin(b*y);
plot3(x,y,zii,'b','LineWidth',4)
hold off

subplot(2,2,3)
plot(zrr+j*zii)
 

Monday, February 22, 2010

3-D view of a waveform

I have used the DFT to decompose the waveform into its components and have used 'plot3' to display the in a 3-D image. The image can be rotated in MATLAB to examine the frequency domain as well as the time domain. The second figure shows the figure rotated.

N=128;n=0:N-1;fund=3/N;%0+.0167/2;
x=sin(2*pi*fund*n)+sin(2*3*pi*fund*n)/3+...
sin(2*pi*5*fund*n)/5+sin(2*pi*7*fund*n)/7;
%x=x.*(hamming(N))';
DFT3D_1(x)

function DFT3D_1(x)
N=length(x);
n=0:N-1;
%x=x.*(hamming(N))';
X=fft(x)*2/N;
X1=abs(X')*ones(1,N);X1=X1';
f=(0:N-1)/N; % Harmonics
[ff,nn]=meshgrid(f,n);
W=cos(2*pi*f'*n);
X1=W'.*X1;
X1(:,1)=x;
plot3(ff,nn,X1,'LineWidth',1);grid on
xlabel('frequency');
ylabel('time seconds');
axis([0 1 0 N -1.3 1.3]);
    view([37.5 30])