# mixing two signals together

120 views (last 30 days)

Show older comments

Hi there, I am trying to mix two signals together in order to display the frequency spectrum. I am running into matrix dimension errors however. How do I fix this? I am fairly new to matlab so if someone could provide code that would be great! Here is what I have so far:

function pushbutton8_Callback(hObject, eventdata, handles)

% hObject handle to pushbutton8 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

load chirp.mat;

fs=8192; % sampling frequency in hz. The "laughter" samples were obtained using Fs=8192. You should not change this value here.

length(y) = 0:1/fs:70000;

lngth2=length(y); % get the number of samples

load handel;

length(y) = 0:1/fs: 70000;

lngth1 = length(y);

lngth3=lngth1 + lngth2; % get the number of samples

t03 = lngth3/fs; % signal duration

t=0:1/fs:t03-1/fs; % create the time index (for t-axis), 0, ts, 2ts,....

ylau3=y(1:lngth3)'; % save the first lngth samples to a variable ylau.

% this step is optional

sound(ylau3,fs); % play the samples

fylau3=fft(ylau3,2*lngth3); % FFT of ylau, this results in the one-sided spectrum

fylaus3=fftshift(fylau3); % fftshift to make the spectrum plot symmetric about f=0, i.e., two-sided spectrum

df3 = fs/length(fylaus3); % create unit frequency step for the frequency-axis (horizontal axis)

fw3=[-fs/2:df3:fs/2-df3]; % frequency range is between -fs/2 to fs/2

y_db3 = 20*log10(abs(fylaus3)./max(abs(fylaus3)));%normalized magnitude spetrum in dB

% axis is in Hz

plot(fw3/1000,y_db3); % obtain the magnitude spectrum plot where frequency axis is in kHz

xlabel('frequency (KHz)');

ylabel('Magnitude (dB)');

title('Frequency spectrum');

grid;

##### 2 Comments

Vittorio
on 28 Apr 2015

### Accepted Answer

Vittorio
on 28 Apr 2015

Edited: Vittorio
on 28 Apr 2015

By the way, "mixing" in signal processing means multiplying. However after looking at the spectrum of the signals I am taking the guess that the problem is meant to show how signal spectra sum. To see what I mean try to run this, then see the difference replacing y3 = y1+y2 with y3 = y1.*y2

load chirp.mat;

y1 = y;

load handel.mat;

y2 = y;

MinimumLength = min([length(y1), length(y2)]);

y1 = y1(1:MinimumLength);

y2 = y2(1:MinimumLength);

y3 = y1+y2;

NFFT = 2^nextpow2(MinimumLength);

f = Fs/2*linspace(0,1,NFFT/2+1);

Y1 = fft(y1,NFFT)/MinimumLength;

figure;

plot(f,2*abs(Y1(1:NFFT/2+1)))

title('Single-Sided Amplitude Spectrum of y(t)')

xlabel('Frequency (Hz)')

ylabel('|Y(f)|')

Y2 = fft(y2,NFFT)/MinimumLength;

figure;

plot(f,2*abs(Y2(1:NFFT/2+1)))

title('Single-Sided Amplitude Spectrum of y(t)')

xlabel('Frequency (Hz)')

ylabel('|Y(f)|')

Y3 = fft(y3,NFFT)/MinimumLength;

figure;

plot(f,2*abs(Y3(1:NFFT/2+1)))

title('Single-Sided Amplitude Spectrum of y(t)')

xlabel('Frequency (Hz)')

ylabel('|Y(f)|')

##### 0 Comments

### More Answers (3)

Vittorio
on 28 Apr 2015

Edited: Vittorio
on 28 Apr 2015

Your code as a number of very basic programming errors. For example:

length(y) = 0:1/fs: 70000;

This makes no sense, since length is a function that returns a value to you, while you are trying to create a vector and assign it to it instead. Are you trying to change the length of y?

If you are trying to mix two signals in a signal processing-type way you just need to multiply them:

load chirp.mat;

y1 = y; % set chirp sound aside

load handel.mat;

y2 = y; % set handel sound aside

MinimumLength = min([length(y1), length(y2)]); % compare the two to find the shortest one

y3 = y1(1:MinimumLength).*y2(1:MinimumLength); % multiply them element-by-element, making sure they are the same length

Then you can do all the fft you want on y1, y2, or y3. To do that I suggest you read carefully through the documentation of fft, looking at the examples.

##### 0 Comments

Søren Jensen
on 28 Apr 2015

fist time answering a question, so i will just copy it down here :P

i did not read through the whole code, but i think i found an error:

length(y) = 0:1/fs:70000;

the command 0:1/fs:70000 basically gives you a vector with the initial value 0 that increments by 1/fs until it reaches 70000 (thus a vector of length fs*70000 = 573440001) and you are trying to put this into a single position in the vector "length".

i believe you are trying to do the following:

y = 0:1/fs:70000

length_y = length(y) % or size(y,2) if your pc also runs out of memory at this call

ALSO:

are you sure the signal is 70000 seconds long?? thats 70000/3600 = 19.44 hours!!

ALSO:

your code for plotting the frequency spectrum seems a little wierd.. if it doesnt work, take a look at this code:

Fs = 8192; L = length(your_signal); NFFT = 2^nextpow2(L);

f = Fs/2*linspace(0,1,NFFT/2+1); spectrum = fft(your_signal,NFFT)/L;

semilogx(f(1:your_choice),2*abs(spectrum(1:your_choice)))

you can also you plot() instead of semilog if you don't like the semilogarithmic plot

- hope this helps

##### 0 Comments

### See Also

### Community Treasure Hunt

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

Start Hunting!