how can i summarize 2 different signals?

Hi all!
I just want to play 2 or more different signals at the same time. If i know well for this, i need to summarize the signals, and the just play back it, but the matrix dimensions musn't agree. Can anyone help me to solve the problem?
Thanks in advance
I tried it with this source code:
clear;
clc;
myFolder = 'C:\Users\Aron\Samples\proba';
if exist(myFolder, 'dir') ~= 7
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav');
wavFiles = dir(filePattern);
sampleArray = cell(length(wavFiles),1);
Fs = zeros(size(sampleArray));
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray{k}, Fs(k)] = wavread(fullFileName);
end
s=1;
data_1=(sampleArray{s});
%sound(data_1);
%%
L = length(data_1);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(data_1,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%%
g=9;
data_2=(sampleArray{g});
L = length(data_2);
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
X = fft(data_2,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
data_sum=Y+X;
data_sum_in_time = ifft(data_sum,NFFT)*L;
sound (data_sum_in_time);

7 Comments

I am not sure what you mean by "summarize" a signal ?
I think you are possibly saying that you have two input signals, usually of different length, and you want to play them simultaneously and for the same duration. Do you want the longer one to be pitch-compressed to the duration of the shorter one? Do you want the shorter one re-sampled to the longer duration ?
I just want to play them at the same time. i don't want to pitch-compressed the longer one. I need a bit help, how can I quantize them to play them at the same time, like a sampler. Do you have any idea?
Thx in advance
If you have the signal processing toolbox, probably easiest would be just to use resample to a common timebase.
Will each of the signals be mono? And to play them "at the same time" would it be okay to play one on the "left" and the other on the "right"? Or are they both stereo and you want them mixed? Or are they both stereo and you want them to be output to different sound cards ?
Well, all of my samples are stereo, and i want to play them at the same time, with add them (mixed them). I have only one sound card. I'm just trying to programing a simple sampler, and therefore i would like to add different signals for playing them at the same time. It's works with same length signals, but i am getting stuck with different length signals. So i want to add the signals from the beginings of each, but i don't know how. And 1 more thing: i don't want to use any toolboxes, just source code.
If sample A is longer than sample B, then after B finishes playing, should the rest of A play by itself, or should one go back to the beginning of B and keep going from there until the end of A ?
Are the samples always taken at the same sampling rate, or does there need to be conversion ?
If sample A is longer than sample B, after B finishes playing, the rest from A should continue playing till end. So it's only need a simple addition from the beginning of each other until the longest sample finishes? (not sure that all sample has the same sample rate, it's a bit complicate the task)
Maybe I need a conversation that convert all sample to the same sample rate. i don't know what should i do if i have a sound library with different length sound signals and doesn't known the sampling rate between the signals and i would like to play them even if more than two.

Sign in to comment.

Answers (2)

Signal A: 10,000 samples, sampled at 20KHz Signal B: 5,000 samples, sampled at 20KHz
A(samples_offset+(1:5000)) = A(samples_offset+(1:5000)) + B; %set the offset to play signal B starting at the same time, ending at the same time, or somethign in the middle.
Signal A: 10,000 samples, sampled at 20KHz Signal B: 5,000 samples, sampled at 10KHz
Here you need to resample B. You can do it via interpolation. - Something like:
C(1:2:9999) = B;
C(2:2:9998) = B(1:end-1) + B(2:end) ./ 2
C(10000) = B(end);

2 Comments

And what if i have a sample library with different samples (and i dont know the sampling rate) and i want to add them, because i would like to play for example 2 or 3 samples at the same time, like a sampler. I need a conversation for all? or what?
Thx in advance
How can you do anything w/ them if the sampling rate isn't known (or at least embedded in the storage format)?
The answer to the question is as given earlier; you have to resample one or both to a common time base in some fashion of your choosing. If you don't want to use the Signal Processing Toolbox resample function, then roll your own...

Sign in to comment.

Respampled = ifft( fft(SampleWithLowerFrequency), ceil(length(SampleWithLowerFrequency) * HigherFrequency / LowerFrequency ) )
now play SampleWithHigherFrequency and Resampled together, both at HigherFrequency

11 Comments

It is doesn't working. I'm just hearing a boom when i'm playing the resampled sample.
Sample 1 Fs is 11025, length is 66068. Sample 10 Fs is 44100 and length is 19962. I would like to adding them to play them at same time. I tried adding 2 samples with the same frequency, but the length is different and its doesn't work too. It was a bit weird, when i divided by 10 the longer one and it is worked. So hard this addition... Pls any idea? And sorry for the comments, it is my nature language.
I tried this code:
clear;
clc;
myFolder = 'C:\Users\Aron\Samples\proba';
if exist(myFolder, 'dir') ~= 7 % ha létezik tovább ugrik, ha nem, hibaüzenetet ad
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav'); % összerakja a teljes fájl nevet
wavFiles = dir(filePattern); % listázza a mappa tartalmát
sampleArray = cell(length(wavFiles),1);
Fs = zeros(size(sampleArray));
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray{k}, Fs(k)] = wavread(fullFileName);
end
s=2; % itt állítom be hanyadik mintát játsza le
data_1=(sampleArray{s})
%sound(data_1);
%% L = length(data_1); % a jel hossza
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(data_1,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%% Hangerő - Volume
d=0; % hangerő dB-ben előjelhelyesen
c=10^(d/20); % hangerő változást okozó szorzó 1üttható
volume_data_1=Y.*c;
Y2 = ifft(volume_data_1,NFFT)*L;
%sound(Y2);
figure(2);
plot(f,2*abs(volume_data_1(1:NFFT/2+1)))
title('Changed Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%% Resampling
Resampled = ifft (fft(sampleArray{1}), ceil(length(sampleArray{1}) * 44100 / 11025 ) );
sound (Resampled);
%% Összegzés
g=10; % hanyadik minta
data_2=(sampleArray{g});
L2 = length(data_2); % Length of signal
NFFT = 2^nextpow2(L2); % Next power of 2 from length of y
X = fft(data_2,NFFT)/L2;
f = Fs/2*linspace(0,1,NFFT/2+1);
data_sum=Y+X;
data_sum_in_time = ifft(data_sum,NFFT)*L;
sound (data_sum_in_time);
It isn't working. I'm just hearing a boom when playing the resampled [signal]. Sample 1 Fs is 11025, length is 66068. Sample 10 Fs is 44100 and length is 19962
That's a 4:1 resample rate; there's no higher frequency content in the lower-sampled one to make anything of higher pitch--you can't create information that isn't there; all you can do is put the same information onto a common time base. Hence, the "boom" is going to be the expected result.
I would like to adding them to play them at same time. I tried adding 2 samples with the same frequency, but the length is different and it doesn't work [either]. ... So hard this addition...
It's really pretty straightforward -- to add two arrays whether they represent audio tracks or whatever, you need the identical length for each. Even "off-by-one" doesn't work. Before trying the addition be certain the two have been adjusted to have the SAME identical length. Oh, also, both have to have the same orientation--either as a row or column vector.
It's easy to check -- if
all(size(A)==size(B))
returns 1 ("true"), then they're compatiable; if it isn't then they're not.
Of course, just that they're dimensionally compatible doesn't help at all w/ the problem of the sampling frequency being so disparate that the result of resampling at such a high multiplier of the original sample rate doesn't make any sense from a practical standpoint. You can have some success in adjusting two slightly different rates to a common base but the Nyquist theorem still holds for the highest reproducible frequency on the original sampling basis for each input.
Thank you dpb, now i understand it. 1 question: if i have sample library with many sounds with the same sampling rate, but differs their lengths. Is it possible if i would like to play many of them at the same time that i just zeroing the rest of the shortest sample length compared to the longest one? Or is there any way to make all sample to the same length? (because of the addition i need) So firstly i have to make all of my samples to the same parameters(sample rate, length), before i begin to operate with them?
First do what you need to do in order to find the maximum sampling frequency out of all of the ones you want to add.
Then
accumulator = [];
for K = 1 : number_of_samples
this_sample = resample sample #K to maximum frequency
if length(accumulator) < length(this_sample)
accumulator(length(this_sample)) = 0;
end
accumulator(1:length(this_sample)) = accumulator(1:length(this_sample)) + this_sample;
end
play accumulator at maximum frequency
Thanks Walter!
I tried it but the dimensions doesn't agree. I am not really understand what this array do: this_sample = resample sample #K to maximum frequency
I tried this: (sample1 has 11025 sampling rate and thats why i tried to the resample (data_1, 8,2) parameters ):
clear;
clc;
myFolder = 'C:\Users\Aron\Samples\proba';
if exist(myFolder, 'dir') ~= 7 % ha létezik tovább ugrik, ha nem, hibaüzenetet ad
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav'); % összerakja a teljes fájl nevet
wavFiles = dir(filePattern); % listázza a mappa tartalmát
sampleArray = cell(length(wavFiles),1);
Fs = zeros(size(sampleArray));
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray{k}, Fs(k)] = wavread(fullFileName);
end
s=1; % itt állítom be hanyadik mintát játsza le
data_1=(sampleArray{s});
%sound(data_1, Fs(s));
%%Resampling
number_of_samples=10;
accumulator = [];
for K = 1 : number_of_samples
this_sample = resample (data_1,8,2); %#K to maximum frequency
if length(accumulator) < length(this_sample)
accumulator(length(this_sample)) = 0;
end
accumulator(1:length(this_sample)) = accumulator(1:length(this_sample)) + this_sample;
end
sound(accumulator, 44100);
What went wrong?
if size(accumulator,1) < size(this_sample,1)
accumulator(length(this_sample),:) = 0;
end
accumulator(1:length(this_sample),:) = accumulator(1:length(this_sample),:) + this_sample;
Since there's no decent subthreading in this forum, it's hard to tell what one is responding to w/o repeating --
... now i understand it. 1 question: if i have sample library with many sounds with the same sampling rate, but differs their lengths. ... if i would like to play many of them at the same time that i just zeroing the rest of the shortest sample length compared to the longest one? Or is there any way to make all sample to the same length?
Zeroing content doesn't do anything about the length; the length would be the same just zero values stored.
You don't say what the format of this "library" is, but since you say they're not the same length one presumes it must be some set of files. Iterate thru that population and read them and then use
length(sample)
and find the overall minimum. Then, to play, add, whatever, use
sample(1:minLeng)
as the portion or make a new waveform that is
newsample=sample(1:minLeng);
and save it to a new set of CommonLengthSampleLibraryFiles and subsequently use those.
But, yes, to do anything useful w/ the disparate sampling rates, durations at same rate, etc., etc., you have to get all to some consistent "lowest common denominator" basis.
Thank you Walter :) Its working with that sample (a bit noisy, coz of the resampling if i guess well), but how can i use this in my code? I don't know what should i do with this array or how can i modify:
this_sample = resample (data_1,1,1);
Could u give me any advice? Or fix my code, Or anything what should i continue, because i still have problems with the addition.
Can u check it?
clear;
clc;
%%Scanning
myFolder = 'C:\Users\Aron\Samples\proba';
if exist(myFolder, 'dir') ~= 7 % ha létezik tovább ugrik, ha nem, hibaüzenetet ad
Message = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(Message));
return;
end
filePattern = fullfile(myFolder, '*.wav'); % összerakja a teljes fájl nevet
wavFiles = dir(filePattern); % listázza a mappa tartalmát
sampleArray = cell(length(wavFiles),1);
Fs = zeros(size(sampleArray));
for k = 1:length(wavFiles)
baseFileName = wavFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
[sampleArray{k}, Fs(k)] = wavread(fullFileName);
end
%%FFT
%Sample 1
s=2; % itt állítom be hanyadik mintát játsza le
data_1=(sampleArray{s});
%sound(data_1);
L = length(data_1); % a jel hossza
NFFT = 2^nextpow2(L); % Next power of 2 from length of y
Y = fft(data_1,NFFT)/L;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(1);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of y(t)')
xlabel('Frequency (Hz)')
ylabel('|Y(f)|')
%Sample 2
g=3; % hanyadik minta
data_2=(sampleArray{g});
%sound(data_2);
L2 = length(data_2); % Length of signal
NFFT = 2^nextpow2(L2); % Next power of 2 from length of y
X = fft(data_2,NFFT)/L2;
f = Fs/2*linspace(0,1,NFFT/2+1);
% Plot single-sided amplitude spectrum.
figure(2);
plot(f,2*abs(Y(1:NFFT/2+1)))
title('Single-Sided Amplitude Spectrum of x(t)')
xlabel('Frequency (Hz)')
ylabel('|X(f)|')
%%Resampling
number_of_samples=10;
accumulator = [];
for K = 1 : number_of_samples
this_sample = resample (data_1,1,1); %#K to maximum frequency
if size(accumulator,1) < size(this_sample,1)
accumulator(length(this_sample),:) = 0;
end
accumulator(1:length(this_sample),:) = accumulator(1:length(this_sample),:) + this_sample;
end
%sound(accumulator, 44100);
%%Volume
d=0; % hangerő dB-ben előjelhelyesen
c=10^(d/20); % hangerő változást okozó szorzó 1üttható
volume_data_1=Y.*c;
Y2 = ifft(volume_data_1,NFFT)*L;
%sound(Y2);
figure(3);
plot(f,2*abs(volume_data_1(1:NFFT/2+1)))
title('Changed Single-Sided Amplitude Spectrum of y2(t)')
xlabel('Frequency (Hz)')
ylabel('|Y2(f)|')
%%Addition
data_sum=Y+X;
data_sum_in_time = ifft(data_sum,NFFT)*L;
sound (data_sum_in_time);
resample(data_1,1,1) is going to give you exactly data_1 back. You should be resampling to match the highest frequency. To get the p, q pair of integers to use for resample(),
[p, q] = rat(Maximum_Frequency ./ Current_Frequency);
@dpl
Why minLeng=min(length(sampleArray{k})) doesn't return the shortest sample?
It's not the shortest one as i see and hear...
The two sample's length values are: l1: 176400 and L2: 17656
And minLeng value is: 19962
min( cellfun(@length, SampleArray) )

Sign in to comment.

Asked:

on 14 Aug 2013

Community Treasure Hunt

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

Start Hunting!