3

I'm trying to sum two signals at the same frequency, one recorded from the microphone and another that it’s a Matlab default sound file, what I'm intending to do is to make them sound at the same time, while I can record, and play the wav file I can’t seem to be able to add them up in the same frequency, for starters Im attempting to put both signals at the same frequency and the same number or rows

However my current problem Im having it’s that i can’t use decimals in the downsample function so i can’t really put them at the same frequency by using it

Up to now I already have the transposed matrix of both Y and grabacion however, I don’t really understand how to put them at the same frequency and number of rows, so i can add them up and then play them as a single sound

enter image description here

load handel.mat

filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs 
   [Y,fs] = audioread('handel.wav');       
sound(Y);
%%
X = X = audiorecorder(8000,8,1);
disp('Inicio de grabacion (5s)')
recordblocking(X, 5);
disp('Fin de Grabacion.');
play(X);
grabacion = getaudiodata(X);     
plot(grabacion,'r-');

%%
Columna_Izquierda = Y(:,1);       
C_I_T  = Columna_Izquierda.'; 

Columna_Derecha = grabacion(:,1);
C_D_T = Columna_Derecha.'; 
GoatZero
  • 219
  • 2
  • 14
  • 1
    What are the dimensions of `y` and `grabacion`? Also how do you know they have been sampled at the same frequency? – Dan Jul 06 '15 at 06:20
  • y is 73113x1 while grabacion is 8000x1 i can control the fs of grabacion by setting a sampling frequency – GoatZero Jul 06 '15 at 07:11
  • 1
    They have to be the same size. So (1) are you certain that they are the same length (in time) when you play them separately? If they are, then you need to adjust your sampling frequency of `grabacion` to match that of `y` (or vice versa) – Dan Jul 06 '15 at 07:30
  • My first problem comes with X = audiorecorder(8000,8,1); 8000 its supposed to be the fs, however when i save the file it actually gets saved as a 40000x1 matrix, so im still trying to figure out whats going on here – GoatZero Jul 06 '15 at 07:36
  • 1
    if `fs` is 8000Hz that means 8000 samples (or data points) per second. You record for 5 seconds. So in total you should expect 8000*5 data points i.e. 40000 – Dan Jul 06 '15 at 07:39

1 Answers1

0

I think you have some confusion about the frequency. Fs is the "sampling frequency", which is how many sound samples are recorded every second. It has no bearing on the frequency of the underlying signal (ignoring aliasing).

If you simply want to add the two sounds together; the first thing you should do is ensure they have the same length. The handle.mat file is 73113/8192 = 8.92 seconds long.

You are recording for 5 seconds, so you should truncate the prerecorded sample to 5 seconds.

Columna_Izquierda = Y(1:8192*5,1); 

Unless your microphone can't handle it, change your recording rate to match this frequency. If you are limited to 8000Hz (which I doubt), you can always use interp1.

X = audiorecorder(8192,8,1);

or

grabacion_interp = interp1(1:8000*5, grabacion, 1:8192*5,'spline');

It's possible you will get some aliasing using spline. I haven't looked at audio filtering in a while.

Then, the dimensions should match so you can add them.

Some revised code

load handel.mat

filename = 'handel.wav';
audiowrite(filename,y,Fs);
clear y Fs 
[Y,fs] = audioread('handel.wav');       
sound(Y);

X = audiorecorder(8192,8,1); % Changed sampling rate
disp('Inicio de grabacion (5s)')
recordblocking(X, 5);
disp('Fin de Grabacion.');
play(X);
grabacion = getaudiodata(X);     
plot(grabacion,'r-');

% Truncate handel recording
Y = Y(1:8192*5);

Columna_Izquierda = Y(:,1);       
C_I_T  = Columna_Izquierda.'; 

Columna_Derecha = grabacion(:,1);
C_D_T = Columna_Derecha.'; 
Dominic A.
  • 31
  • 2