I have to create beep arrays of differing numerosity. I determine how many beeps will be
played, the duration of each individual beep and the inter-beep duration online. However,
these all have to be played really fast, both the intervals and the durations will be in between 0.01secs-0.2secs. The sound function of matlab does not allow you to take very small breaks in between as even if you play two sound functions one after each other without putting a small break by a timer, it takes longer than the timing I need. Any ideas? I would really appreciate any help...
To guarantee a given spacing, I think that you need to assemble the waveform. I suspect a lot of variation in the time it takes to startup the sound function.
As an example, the function below should give you a good start:
function beep_series(specs)
%BEEP_SERIES Create a series of beeps
% BEEP_SERIES([FREQ_HZ, VOL, DUR_SEC, PAUSE_SEC]) creates a series of
% beeps from an N-by-4 spec matrix, where the colums represent:
% frequency
% volumne (0-1)
% beep duration
% pause duration,
freq = specs(:,1);
volume = specs(:,2);
duration = specs(:,3);
trailingGap = specs(:,4);
SAMPLE_FREQ = 8192;
totalTime = sum(duration) + sum(trailingGap);
x = zeros(ceil(totalTime*SAMPLE_FREQ),1);
curBeepStartTime = 0;
for ix = 1:length(freq)
numSamples = round(duration(ix)*SAMPLE_FREQ);
x( round(curBeepStartTime*SAMPLE_FREQ + (1:numSamples)) ) = ...
volume(ix) * sin( (1:numSamples) * (2*pi*freq(ix)/SAMPLE_FREQ) );
curBeepStartTime = curBeepStartTime + duration(ix) + trailingGap(ix);
end
sound(x, SAMPLE_FREQ)
Related
I'm attempting to create a reverb function in MATLAB and would like to filter the repeats to make them sound more natural. I have the signal processing toolbox however I get an error telling me that I cannot use the lowpass() function on single samples and MATLAB wigs out. The idea is to filter the signal every time it enters the feedback loop to simulate the absorption of high frequencies every time the sound reflects off a surface. My code looks like this:
[x,Fs] = audioread('Clap.mp3');
%Calculate delay length
secOfDelay = 0.03; %units of seconds
samplesOfDelay = secOfDelay * Fs; %sec * samples/sec = samples
%Gain for wet mix
g = 0.9;
for n = 1:length(x)
if (n - samplesOfDelay) < 1 %For no past samples, don't add the delay yet.
out(n,1) = x(n,1);
else
a(n,1) = x(n,1);
b(n,1) = x(n,1) + (g * b(n-samplesOfDelay,1));
end
end
%Add wet mix and dry mix together
out = a+b;
sound(out,Fs);
Any help would be greatly appreciated!
JDB
I'm trying to create some functions in Matlab that is going to record a tone and then play it back semi-continuous. For example I want the user to be able to whistle for 2 seconds and the whistle to be dragged out to 10 seconds. I want to avoid using fancy time stretch algorithms but just use this through repetitions. I know there's issues with this but I'm doing this for experiment.
To do this, I need to crossfade the beginning and end of the sound to make it seem continuous. I have written a crossfade function that takes two signals and fades between them.
The idea is to have a sigmoid volume function that goes from 1 to 0 for the old sound and 0 to 1 for the new. I then just multiply the signal by this volume function and add the result. My understanding of the sigmoid function is that the resulting volume should be an average of the two input signals, with weight slowly moving from input signal a to input signal b, and the output amplitude should be the same as the input ones.
When I try this with two simple sinusoid input signals, however, and go back and forth between a low and high tone, I get a small "glitch" in the section just where it transfers from a low-to-high section, to a high-to-low sliding section. Where does this come from? I'm also not sure if this is only on my speakers, and I tried watching the plotted waveforms in time domain, but couldn't see anything weird happening at the specific time.
The end of the first section, and beginning of the last should have exactly the same frequency component, and nothing is moved in time, so I'm not sure where this glitch comes from.
Sample code:
(SLIGHT WARNING: This output some tones, one of which is a little bit high pitch, so maybe not play this with earphones on or with full volume).
function test()
Fs=44100;
x=1:1/Fs:2;
x=x'; %time vector
freq1 = 440; %low tone
freq2 = freq1*2^(7/12) % high tone (harmonic fifth)
Fs=44100;
x=1:1/Fs:2;
x=x'; %time vector
freq1 = 440; %low tone
freq2 = freq1*2^(7/12); % high tone (harmonic fifth)
y1 = sin(2*pi*x*freq1)
y2 = sin(2*pi*x*freq2)
res = stitchandrepeat(y1,y2, Fs);
end
function [out] = stitchandrepeat(in, in2, Fs)
lowtohigh = crossfade(in,in2,1);
hightolow = crossfade(in2,in,1);
out = [lowtohigh;hightolow];
out = [out;out];
out = [out;out];
out = [out;out];
end
function [out] = crossfade(a, b, sigmoid)
if not(size(a) == size(b))
error('Different sizes');
end
index = (1:size(a,1))';
factor = index/size(a,1);
if(sigmoid)
steepness = 5;
factor = logsig((factor-0.5)*steepness*2);
end
plot(index, factor, 'o');
out = bsxfun(#times, a, 1-factor)+bsxfun(#times, b, factor);
end
I even tried doing a second crossfade where I stitched together the end and beginning of the first and second signal, but it still sounded a little bit weird just in the transition. Any idea on how to make the transition smooth?
function [out] = stitchandrepeat2(in, in2, Fs)
lowtohigh = crossfade(in,in2,1);
hightolow = crossfade(in2,in,1);
secstofade = 0.1;
len = size(lowtohigh,1);
partsize = secstofade*Fs;
lthpart = lowtohigh((len-partsize+1):len,:);
htlpart = hightolow(1:partsize,:);
lthcropped = lowtohigh(1:len-partsize,:);
htlcropped = hightolow(partsize+1:len,:);
highfade = crossfade(htlpart, lthpart,1);
out = [lthcropped;highfade;htlcropped];
out = [out;out];
out = [out;out];
out = [out;out];
end
And how come I don't get this glitch sound in the low part of the sound? (Two different sound waves are also just concatenated there.)
Below you can see a function which generates pure sinusoidal waveform. I need to modify it to generate sawtooth waveform. Help please! (I'm on Matlab 2014b with Psychtoolbox 3.0.12, Win 8.1 x64)
function [tSnd] = mkPureTone(fs, frq, dur)
if nargin < 1; fs = 44100; end
if nargin < 2; frq = 1000; end
if nargin < 3; dur = 0.1; end
rtimeup = 0.006; % duration of the ramp up in seconds
rtimedown = 0.006; % duration of the ramp down in seconds
trup = 0:1/fs:rtimeup-1/fs;
lrup = length(trup);
rampup = (cos(2*pi*trup/rtimeup/2+pi)+1)/2;
trdown = 0:1/fs:rtimedown-1/fs;
lrdown = length(trdown);
rampdown = (cos(2*pi*trdown/rtimedown/2)+1)/2;
% compute target sound
time = 0:1/44100:dur;
sound = sin(2*pi*frq*time);
lt = length(sound);
sound(1:lrup) = sound(1:lrup).*rampup;
sound(lt-lrdown+1:end) = sound(lt-lrdown+1:end).*rampdown;
tSnd = sound;
% wavplay(tSnd, fs);
end
The simple way to go would be to replace
sound = sin(2*pi*frq*time);
with something like
sound = mod(frq*time,1);
This will create a sawtooth at frequency frq and of amplitude 1 (i.e. between 0 and 1). You can change the amplitude of the waveform (by multiplying it with a scalar) or shift it (by adding a scalar), depending on your needs.
But this modification won't take into account the rising and falling ramps. The way it is handled in the code you show seems rather strange to me, especially I don't get why one needs to use the cos function, so I don't touch that part of the code.
Best,
Thanks. Colleague of mine also looked into it and his idea was:
sound = time*frq - floor(time*frq)
Both this and your version sound indistinguishably, but when I compare it to the sawtooth wav generated online, this Matlab sound sounds less clear and it begins and ends with small but audible click. Not sure if it's a matter of driver or of the generated sound itself.
%-------------------------------------------------------------------
% Function to Generate ECG of 1 heart beat signal
function [Heartbeat,t1] = ECG_Gen (HR,pulse_width,Amp)
Fs = 48000;
delay = ((60/HR)/2)-(0.5*pulse_width);
t1 = -delay:(1/Fs):delay;
Heartbeat = Amp*tripuls (t1,pulse_width);
%-------------------------------------------------------------------
%Test Circuit configuration
function [FECG_MECG,Mixed_ECG,fastTime] = Test_Circuit (FHR,MHR)
Fs = 48000;
%FHR = 150;
%MHR = 60;
Fpulse_width = 30e-3;
Mpulse_width = 60e-3;
FAmp = 0.2;
MAmp = 0.5;
% Fetal ECG Gen
%------------------------------------------------
[FECG,FHR_Delay]= ECG_Gen (FHR,Fpulse_width,FAmp);
% Maternal ECG Gen
%------------------------------------------------
[MECG,MHR_Delay]= ECG_Gen (MHR,Mpulse_width,MAmp);
% Composite signal implementation
%------------------------------------------------
% Set parameters for Composite signal Algorithms
if length (MECG) > length (FECG) % Check for time sequences for both ECG signal
slowECG = FECG; % Set interpolation to slower rate
fastECG = MECG;
timeSeg = length(MECG);
fastTime = MHR_Delay; % Set sampling times
slowTime = FHR_Delay;
else
slowECG = MECG;
fastECG = FECG;
timeSeg = length(FECG);
fastTime = FHR_Delay;
slowTime = MHR_Delay;
end
FECG_MECG = zeros (timeSeg,2); % To hold stereo output
FECG_MECG(:,2) = fastECG(1,:); % Assign higher rate signal to one channel
% Interpolation on the slower rater sampled ECG
slowECGInterp = interp1 (slowTime,slowECG,fastTime);
slowECG = num2cell(slowECGInterp); % Conversion to cell Array in order to remove NaN
slowECG(cellfun(#(slowECG) any(isnan(slowECG)),slowECG)) = [];
slowECG = cell2mat(slowECG);
j = 1;
for i = 1:timeSeg
FECG_MECG(i,1) = slowECG(1,j);
if j == length(slowECG)
j = 0;
end
j = j+1;
end
Mixed_ECG = FECG_MECG(:,1) + FECG_MECG(:,2); % to hold mono output
%while (1)
%sound(Mixed_ECG ,Fs);
%end
%-------------------------------------------------------------------
% Test Wave script
clear all
%clc
clc
Fs = 48000;
%for i = 1:3
%toc
MHR = 60;
FHR = 200;
% Obtain ECG interpolated signal and composite
[FECG_MECG,Mixed_ECG,fastTime] = Test_Circuit (FHR,MHR);
% for test purposes
[MECG,MHR_Delay]= ECG_Gen (60,60e-3,0.5);
%t = timer ('TimerFcn','stat=false','Period',2.0);
wavwrite (FECG_MECG(:,2),Fs,'ECGwav.wav');
i = 0;
a = 1;
tic
while (1)
while (toc < 20*a)
sound (MECG,Fs);
end
toc
a = a+1;
[MECG,MHR_Delay]= ECG_Gen (60*a,60e-3,0.5);
if a > 4
break
end
end
%start(t)
%tic
%t = cputime;
%y = wavread('ECGwav.wav');
%while (1)
% sound(y,Fs);
%end
%toc
Hey Thank you very much for getting back to me, I have made use of your interpolation but still have minor problems from the reading obtained from the Monitor. Fist of all, say I have a constant signal with fixed time period say, 0.8s and I want to add composite signal of say 0.3s, I have managed to use your interpolation method to sample the 0.3s signal at the rate of my 0.8s signal. I think I have solved this issue. Second issue deals with how to combine the two signals which I have managed to do somehow but when I use a while loop in order to repeat the composite signal say over 20s, the signals obtained from the sound output isn't quite what I expected since its sounding the array of stored composite signal which contain ( signal with 0.8s = slowInterp signal of 0.3s ). I have include all the codes and functions. Basically, I need the sound output in while loop to sync with the composite signal for example: if I have a signal that repeats every 1s, I would expect to hear a beep in a while loop that runs for 10s to produce 10 beeps, etc
It is hard to tell exactly what you need, but probably doing interpolation on the sequence that is sampled at the slower rate would work for your application.
If t1s are your times from your faster sequence, and t2s are times from your slower sequence, and slow is your slower sequence then do:
slowInterp = interp1(t2s, slow, t1s);
now you will have the sequency slow sampled at the faster rate.
This is only useful for displaying the time series. If you are doing any spectral analysis, this will introduce artifacts, but that is a more advanced topic.
Using the resample function in the signal processing toolbox could also be useful.
I'm having trouble getting consistent results with the code I am using. I want to run my Arduino for a specific amount of time (say 20 seconds) and collect data from the analog pin with a specific sampling rate (say four samples a second). The code is as follows.
a_pin = 0;
tic;
i = 0;
while toc < 20
i = i + 1;
time(i) = toc;
v(i) = a.analogRead(a_pin);
pause(.25);
end
Is there a way to set the loop to run a specific time and then in the loop sample at a different rate?
You can try this:
a_pin = 0;
fs = 4; % sampling frequency (samplings per second)
mt = 20; % time for measurements
ind = 1;
nind = 1;
last_beep = 0;
tic;
while toc < mt
time(ind) = toc;
v(ind) = a.analogRead(a_pin);
% wait for appropriate time for next measurement
while( nind == ind )
nind = floor(toc*fs) + 1;
end
ind = nind;
% beep every second
if (ceil(toc) > last_beep)
beep(); % don't know if this one exist, check docs
last_beep = ceil(toc);
end
end
Maximal sampling time for a single Arduino analog read command is around 0.04 s, in practice I'd go minimally 0.05. Adding two read operations is in the order of 2*0.04, in practice more like 0.1 s. I think it is mainly limited by the USB communication speeds.
I am also new at arduino, but having implemented a real time analysis for EEG using it, on practice, I was able to sample 2 analog channels with a samplinf frequency between 57 and 108Hz. It was very variable (calculated through tic/toc), but it is still proper for realtime processing in my case.
My code uses a While loop, a series of memory updates, digital pin manipulations, plot of trace (drawnow) and seems to run smoothly enough
My answer is simply here : 0.0283 sec for sampling 2 analog inputs in my case.
Cheers