auto-correlation in matlab - matlab

i want to ask about how to do auto-correlation function to every frame in a sound wave by my hand not built in function i do a code and i don't know if it right or wrong so please help
also i want to get the pitch period
[sound, Fs]= wavread('aa.wav');
subplot(5,1,1);plot (sound);title('The Orignal Wave')
frameSize= 10/10^3;
overlapSize= 5/10^3;
frameSize_samples = round(Fs*frameSize);
overlapSize_samples= round(overlapSize* Fs);
shift = frameSize_samples-overlapSize_samples;
nframes=ceil((length(sound)-frameSize_samples)/shift);
frames = zeros(nframes, frameSize_samples);
for i=1:nframes
frames(i,:) = sound( ((i-1)*shift+ 1):((i-1)*shift+ frameSize_samples) )';
end
subplot(5,1,2);plot (frames(:));title(' Wave after framing')
w = rectwin(frameSize_samples)';
frames1 = zeros(nframes, frameSize_samples);
for i=1:nframes
frames1(i,:)=(frames(i,:).*w);
end
%/////////////////////////////////////////////////////////
% calc energy
numSamples = length(sound);
energy = zeros(1,nframes);
for frame = 1:nframes
energy(frame) = sum(frames1(frame).^2); %# Calculate frame energy
end
subplot(5,1,3);plot (energy(:));title(' Energy')
% calc autocorrelation
autocorrelation = zeros(1,nframes);
for i=1:nframes
for k = 0:frameSize_samples
autocorrelation(i,:)= sum(frames(i)*frames(i+k));
end
end
subplot(5,1,4);plot (autocorrelation(:));title(' autocorrelation')

Based on the equation here. The autocorrelation equation looks fine. But I am confused by frames. You define it as a 2-D matrix, but then index it linearly (frames(i)).
Also, the for loop for k=0:frameSize_samples is not doing anything; you are assigning the same variable autocorrelation(i, :) in every iteration, so it will be overwritten.

Related

Matlab sound with a function for a keyboard [duplicate]

Hello I have to build a program that is able to play some piano sounds from beethoven elise
Now I have implemented the function for the frequency and the sound but it sounds strange and not at all like piano notes.
Could someone give me advice on how to fix this?
createWaveform(440,8000,2,1);
function [sinusoid] = createWaveform(frequency,fs,duration,A)
n = linspace(0,duration-1/fs,duration*fs);
sinusoid = 1:duration*fs;
for i = 1:duration*fs
sinusoid(i) = A*cos(2*pi*frequency*n(i)*(1/fs)+(2*pi).*rand(1,1));
end
end
end
Here is the part where I am attempting to build tones to the right piano note
%function [tone] = note(keynum,relDuration,fullDuration,fs)
%basetone = 440;
% frequency = basetone * nthroot(2,12)^(keynum-49);
%[tone]=createWaveform(frequency,fs,relDuration*fullDuration,1);
%end
Edit: I've deleted all my old code and wrote the following that did the job
%sound(createWaveform(1000,8000,1,1));
sound(note(24,1/2,1,8000));
function [sinusoid] = createWaveform(frequency,fs,duration,A)
Fs = fs; % samples per second
dt = 1/Fs; % seconds per sample % seconds
t = (0:dt:duration)'; % seconds
%%Sine wave:
Fc = frequency; % hertz
sinusoid = A.*cos(2*pi*Fc*t+(2*pi).*rand(1,1));
% Plot the signal versus time:
end
function [tone] = note(keynum,relDuration,fullDuration,fs)
basetone = 440;
frequency = basetone * nthroot(2,12)^(keynum-49);
tone=createWaveform(frequency,fs,relDuration*fullDuration,0.5);
end
function [E] = envel(relDuration,fullDuration,fs)
param = 0;
for i = 0:fs:relDuration*fullDuration
E(i)=relDuration*fullDuration;
i=i+1;
end
end
As mentioned in the Comments, the sound that a piano makes is quite complicated. As such this is not a Matlab challenge, just a math problem.
Now that you know this, you 'just' need to come up with a reasonable mathematical approximation, and from here it should be straight forward to implement in Matlab.
Here is something to get you started with the math:
https://dsp.stackexchange.com/questions/46598/mathematical-equation-for-the-sound-wave-that-a-piano-makes

What's wrong with my code in Matlab?

I want to play all frequencies given in matrix(FrTm) with its duration.The actual duration is one second but each frequency has to play for 3 of 1/18 and 6 of 1/18 seocnd such as given in matrix(FrTm).
function Music()
Fs=44100;
T=1/Fs;
M = zeros(1,88);
for I=7:88,
M(I) = round(36.8*(2^(1/12))^(I-6));
end
Signal=[];
FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];
t=0:1/18:1;
for i=1:length(FrTm),
M(i)=FrTm(i);
Z=M(i);
data= sin(2*pi*Z/Fs*t);
signal=[data;signal];
end
stem(Signal);
sound (Signal, 44100);
end
The classical way to make a sound with a given frequency (f) and sample frequency (Fs) is to make a time vector with step 1/Fs:
time = 0:1/Fs:D;
Where Dis the duration of the signal. The signal itself is then:
signal = sin(2*pi*f.*time)
In this case the total time is fixed, not the time of each signal. The total time is denoted with T, and the total time vector is made as
time = 0:1/Fs:T;
The sum of the second column is the total number of units the vector time needs to be divided in, e.g. 50, 3 means that a signal at 50 Hz needs to be played for 3 units. This means we only need a time vector of the length of 3 units:
t = time(1:floor(end*duration/s));
Where duration is the number of units for this part and s is the total number of units. The signal is then simply, as stated above,
data = sin(2*pi*f*t);
The data is then appended to the whole signal. The complete code, looks like this:
Fs = 44100; % sample frequency [Hz]
T = 3; % total duration [s]
time = 0:1/Fs:T;
% matrix with frequencies and duration
FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];
s = sum(FrTm(:,2));
[m, ~] = size(FrTm);
signal = [];
for i=1:m
freq = FrTm(i,1);
duration = FrTm(i,2);
t = time(1:floor(end*duration/s));
data = 10*sin(2*pi*freq.*t);
signal = [data signal];
end
stem(signal);
sound(signal, 44100);
Note instead of declaring time in the beginning, it is possible to make a new vector each time you run through the loop. In that case omit time = 0:1/Fs:T; and change t = time(1:floor(end*duration/s)); to t = 0:1/Fs:floor(end*duration/s);
function Music()
Fs=44100;
T=1/Fs;
M = zeros(1,88);
for I=7:88,
M(I) = round(36.8*(2^(1/12))^(I-6));
end
Signal=[];
FrTm=[50 3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];
for i=1:length(FrTm),
%---------------------------------------------------
% complete the function
freq = FrTm(i,1);
duration = FrTm(i,2);
time =0:1/Fs:1; % change the 1 to change total duration
s = sum(FrTm(:,2));
t = time(1:floor(end*duration/s));
data = sin(2*pi*freq.*t);
Signal = [data Signal];
end
stem(Signal);
sound (Signal, 44100);
end
This is the exact code what i wanted ViG can you please remove this tak tak sound it just a noise actually how to use envelope function to remove thid tak tak sound in music code is following.
Fs=44100;
T=1/Fs;
M=zeros(1,88);
for I=7:88
M(I)=round(36.8*(2^(1/12))^(I-6));
end
signal=[];
FrTm=[50,3;50,3;52,3;54,3;50,3;54,3;52,3;45,3;50,3;50,3;52,3;54,3;50,6;
49,3;1,3;50,3;50,3;52,3;54,3;55,3;54,3;52,3;50,3;49,3;45,3;47,3;49,3;50,6;
50,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;1,3;45,5;47,1;45,3;43,3;42,6;
45,3;1,3;47,5;49,1;47,3;45,3;47,3;49,3;50,3;47,3;45,3;50,3;49,3;52,3;50,6;
50,6];
for i=1:length(FrTm)
x=FrTm(i,1);
y=FrTm(i,2);
F=M(x);
time=0:1/Fs:y/18;
sinewave=sin(2*pi*F*time);
signal=[signal sinewave];
end
stem(signal)
sound(signal,Fs)

Matlab 1D wave equation FDM second order in time, fourth order in space

I wrote a function to solve the 1D wave equation with FDM. Therefore i used second order accuracy in time and fourth order in space and an explicit FD scheme.
I already implemented the solver function in Matlab with an matrix-vector-multiplication approach (alternative this can be done iterative) with periodic boundary conditions.
To verify the code i used the Method of Manufactured Solution. My approach is to assume the solution as
p(t,x)=sin(x+t)+sin(x-t)
which is periodic and sufficient smooth and differentiable. I implemented a source term f which is as well as the initial data input for the following function
function [x,t,P_End]= MMS(f,I,G,L,v,T,J,CFL,x)
% Initialisation
deltax=x(2)-x(1);
deltat=CFL*deltax/abs(v);
c=(v*deltat/deltax)^2;
t=(0:deltat:T);
N=length(t);
A=zeros(J,J);
for k=1:J
% periodic boundary condition
if k==1
A(1,1)=-c*5/2;
A(1,2)=c*4/3;
A(1,3)=c/12;
A(1,J-1)=c/12;
A(1,J)=c*4/3;
elseif k==J
A(J,1)=c*4/3;
A(J,2)=c/12;
A(J,J-2)=c/12;
A(J,J-1)=c*4/3;
A(J,J)=-c*5/2;
elseif k==2
A(2,J)=c/12;
A(2,1)=c*4/3;
A(2,2)=-c*5/2;
A(2,3)=c*4/3;
A(2,4)=c/12;
elseif k==J-1
A(J-1,1)=c*1/12;
A(J-1,J-1)=-c*5/2;
A(J-1,J)=c*4/3;
A(J-1,J-2)=c*4/3;
A(J-1,J-3)=c*1/12;
else
A(k,k-2)=c/12;
A(k,k-1)=c*4/3;
A(k,k)=-c*5/2;
A(k,k+1)=c*4/3;
A(k,k+2)=c/12;
end
end
%Allocate memory
P_0=zeros(J,1);
b=zeros(J,1);
H=zeros(J,1);
%Initial data read in
for i=1:J
P_0(i)=I(x(i));
b(i)=f(x(i),t(1));
H(i)=G(x(i));
end
%Calculation of first time step separate because to time steps back
%are needed in the iteration
P_1=0.5*A*P_0+(deltat^2/2)*b+2*deltat*H+P_0;
P_n_minus_1=P_0;
P_n=P_1;
P_End=zeros(N,J); % Solution matrix
P_End(1,:)=P_0;
P_End(2,:)=P_1;
for n=2:N
for i=1:J
b(i)=f(x(i),t(n));
end
%Iterative calculation for t_2,...,t_N
P_n_plus_1=A*P_n+(deltat^2)*b-P_n_minus_1+2*P_n;
%Overwriting
P_n_minus_1=P_n;
P_n=P_n_plus_1;
P_End(n,:)=P_n_plus_1;
end
end
The function call is then
clear all; clc; close all;
%% Initialisierung
% Grid points in space x_0,...x_L
x = -2 : 0.01 : 2;
J = length(x);
xDelta = x(2) - x(1);
T = 2;
v = 0.5; %velocity constant
CFL = 0.5; %Courant Friedrich Lewis number
%Source term right-hand side of the wave equation
f = #(x,t) abs(v^2-1)*(sin(x+t)+sin(x-t));
%Initial data for the estimated sound pressure function p(t,x), t=0
I = #(x) 2*sin(x);
% \partial p/ \partial t , t=0
G = #(x) 0;
[x,t,P_End]= MMS(f,I,G,v,T,J,CFL,x);
This initial data and source term input leads to a solution that proceed like the assumed solution but in range ob `+/- 10^24.
What an i doing wrong here? I already reviewed the code hundred of times but could not detect any code mistakes.
Thanks for any hints!

How to store values in an array in MATLAB

Say I have performed FFT on a set of data and i have the frequency and amplitude values.
I want to find the highest amplitude in each FFT block. The I need to find the frequency and amplitude of the points that are atleast greater than 0.4 times the max amplitude and save them in an array.. How can i do this??
I tried the following method by I keep getting an empty matrix....
% the code after FFT
peak_points = [];
fmin = 60;
fmax = 1000;
region_of_interest = fmax>f & f>fmin;
froi = f(region_of_interest);
[p_max,loc] = max(seg_fft2(region_of_interest));
p_max;
fpeaks = froi(loc);
[points, locatn] = findpeaks(seg_fft2(region_of_interest));
if points > 0.4*p_max
peak_points = [peak_points ; points locatn]
end
Im bad with arrays.. So I cant seem to figure this out. Would appreciate someone's help in this... Thanks in advance...
Are you intending to do the seg_ffr2 on the region of interest logical array or on froi?
or maybe points is vector and you should have:
aboveMax = points > 0.4*p_max;
if any(aboveMax)
peak_points = [peak_points ; points(aboveMax) locatn(aboveMax)]
end

How can I output a composite signal made of two signals with different time period at the same time on one audio channel in matlab code?

%-------------------------------------------------------------------
% 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.