Here is the context of the problem: I have a DTMF signal in wav format, I have to identify the number sequence it has encoded. I must do so using fast fourier transform in Matlab, implying that I read the wav file using wavread and to identify each number that is seperated by 40ms silence or more.
Here is my code so far:
[signal, fs] = wavread( 'C:\Temp\file.wav' ); % here, fs = 8000Hz
N = 512;
T = 1/fs;
L = length( signal )
samples = fs / 1000 * 40
windows = floor(L / samples)
t = (1:L)/fs;
figure(1), plot(t, signal);
Here is what the figure 1 looks like, that is the signal read from the wav:
How can I effectively split the signal into pieces so that I can then do an FFT on each of the 10 pieces seperately to decode the corresponding numbers?
I would recommend the following approach:
Find the envelope of the signal in the time domain (see Hilbert transform).
Smooth the envelope a bit.
Take the diff and find peaks to get the onsets of the tones.
Use the onsets to pick frames and find the spectrum using fft.
Find the index of the max in each of the spectrums and convert them to a frequency.
The tricky part in this is to get a robust onset detector in point 3. The peaks in the difference you pick, has to be of a certain size in order to qualify as on onset. If your tones are of varying strength this might pose a problem, but from your image of the time signal it doesn't seem like a problem.
Regards
This worked for me:
windowSize = 256;
nbWindows = floor(L / windowSize);
for i=1:nbWindows
coeffs = fft(signal((i-1)*windowSize+1:i*windowSize));
plot(abs(coeffs(1:N)));
waitforbuttonpress
end;
This way it is possible to shift the window until the end of the input signal
Related
I am writing a piece of code that figures out what frequencies(notes) are being played at any given time of a song (note currently I am testing it grabbing only the first second of the song). To do this I break the first second of the audio file into 8 different chunks. Then I perform an FFT on each chunk and plot it with the following code:
% Taking a second of an audio file and breaking it into n many chunks and
% figuring out what frequencies make up each of those chunks
clear all;
% Read Audio
fs = 44100; % sample frequency (Hz)
full = audioread('song.wav');
% Perform fft and get frequencies
chunks = 8; % How many chunks to break wave into
for i = 1:chunks
beginningChunk = (i-1)*fs/chunks+1
endChunk = i*fs/chunks
x = full(beginningChunk:endChunk);
y = fft(x);
n = length(x); % number of samples in chunk
amp = abs(y)/n; % amplitude of the DFT
%%%amp = amp(1:fs/2/chunks); % note this is my attempt that I think is wrong
f = (0:n-1)*(fs/n); % frequency range
%%%f = f(1:fs/2/chunks); % note this is my attempt that I think is wrong
figure(i);
plot(f,amp)
xlabel('Frequency')
ylabel('amplitude')
end
When I do that I get graphs that look like these:
It looks like I am plotting too many points because the frequencies go up in magnitude at the far right of graphs so I think I am using the double sided spectrum. I think I need to only use the samples from 1:fs/2, the problem is I don't have a big enough matrix to grab that many points. I tried going from 1:fs/2/chunks, but I am unconvinced those are the right values so I commented those out. How can I find the single sided spectrum when there are less than fs/2 samples?
As a side note when I plot all the graphs I notice the frequencies given are almost exactly the same. This is surprising to me because I thought I made the chunks small enough that only the frequency that's happening at the exact time should be grabbed -- and therefore I would be getting the current note being played. If anyone knows how I can single out what note is being played at each time better that information would be greatly appreciated.
For a single-sided FT simply take the first half of the output of the FFT algorithm. The other half (the nagative frequencies) is redundant given that your input is real-valued.
1/8 second is quite long. Note that relevant frequencies are around 160-1600 Hz, if I remeber correctly (music is not my specialty). Those will be in the left-most region of your FT. The highest frequency you compute (after dropping the right half of FFT) is half your sampling frequency, 44.1/2 kHz. The lowest frequency, and the distance between samples, is given by the length of your transform (44.1 kHz / number of samples).
I am currently doing a thesis that needs Ultrasonic pulse velocity(UPV). UPV can easily be attained via the machines but the data we acquired didn't have UPV so we are tasked to get it manually.
Essentially in the data we have 2 channels, one for the transmitter transducer and another for a receiver transducer.
We need to get the time from wave from the transmitter is emitted and the time it arrives to the receiver.
Using matlab, I've tried finddelay and xcorr but doesnt quite get the right result.
Here is a picture of the points I would want to get. The plot is of the transmitter(blue) and receiver(red)
So I am trying to find the two points in the picture but with the aid of matlab. The two would determine the time and further the UPV.
I am relatively a new MATLAB user so your help would be of great assistance.
Here is the code I have tried
[cc, lags] = xcorr(signal1,signal2);
d2 = -(lags(cc == max(cc))) / Fs;
#xenoclast hi there! so far the code i used are these.
close all
clc
Fs = input('input Fs = ');
T = 1/Fs;
L = input('input L = ');
t = (0:L-1)*T;
time = transpose(t);
i = input('input number of steploads = ');
% construct test sequences
%dataupv is the signal1 & datathesis is the signal2
for m=1:i
y1 = (dataupv(:,m) - mean(dataupv(:,m))) / std(dataupv(:,m));
y2 = (datathesis(:,m) - mean(datathesis(:,m))) / std(datathesis(:,m));
offset = 166;
tt = time;
% correlate the two sequences
[cc, lags] = xcorr(y2, y1,);
% find the in4dex of the maximum
[maxval, maxI] = max(cc);
[minval, minI] = min(cc);
% use that index to obtain the lag in samples
lagsamples(m,1) = lags(maxI);
lagsamples2(m,1) = lags(minI);
% plot again without timebase to verify visually
end
the resulting value is off by 70 samples compared to when i visually inspect the waves. the lag resulted in 244 but visually it should be 176 here are the data(there are 19 sets of data but i only used the 1st column) https://www.dropbox.com/s/ng5uq8f7oyap0tq/datatrans-dec-2014.xlsx?dl=0 https://www.dropbox.com/s/1x7en0x7elnbg42/datarec-dec-2014.xlsx?dl=0
Your example code doesn't specify Fs so I don't know for sure but I'm guessing that it's an issue of sample rate(s). All the examples of cross correlation start out by constructing test sequences according to a specific sample rate that they usually call Fs, not to be confused with the frequency of the test tone, which you may see called Fc.
If you construct the test signals in terms of Fs and Fc then this works fine but when you get real data from an instrument they often give you the data and the timebase as two vectors, so you have to work out the sample rate from the timebase. This may not be the same as the operating frequency or the components of the signal, so confusion is easy here.
But the sample rate is only required in the second part of the problem, where you work out the offset in time. First you have to get the offset in samples and that's a lot easier to verify.
Your example will give you the offset in samples if you remove the '/ Fs' term and you can verify it by plotting the two signals without a timebase and inspecting the sample positions.
I'm sure you've looked at dozens of examples but here's one that attempts to not confuse the issue by tying it to sample rates - you'll note that nowhere is it specified what the 'sample rate' is, it's just tied to samples (although if you treat the 5 in the y1 definition as a frequency in Hz then you will be able to infer one).
% construct test sequences
offset = 23;
tt = 0:0.01:1;
y1 = sin(2*pi*5*tt);
y2 = 0.8 * [zeros(1, offset) y1];
figure(1); clf; hold on
plot(tt, y1)
plot(tt, y2(1:numel(tt)), 'r')
% correlate the two sequences
[cc, lags] = xcorr(y2, y1);
figure(2); clf;
plot(cc)
% find the index of the maximum
[maxval, maxI] = max(cc);
% use that index to obtain the lag in samples
lagsamples = lags(maxI);
% plot again without timebase to verify visually
figure(3); clf; hold on
plot(y1)
plot(y2, 'r')
plot([offset offset], [-1 1], 'k:')
Once you've got the offset in samples you can probably deduce the required conversion factor, but if you have timebase data from the instrument then the inverse of the diff of any two consecutive entries will give it you.
UPDATE
When you correlate the two signals you can visualise it as overlaying them and summing the product of corresponding elements. This gives you a single value. Then you move one signal by a sample and do it again. Continue until you have done it at every possible arrangement of the two signals.
The value obtained at each step is the correlation, but the 'lag' is computed starting with one signal all the way over to the left and the other overlapping by only one sample. You slide the second signal all the way over until it's only overlapping the other end by a sample. Hence the number of values returned by the correlation is related to the length of both the original signals, and relating any given point in the correlation output, such as the max value, to the arrangement of the two signals that produced it requires you to do a calculation involving those lengths. The xcorr function makes this easier by outputting the lags variable, which tracks the alignment of the two signals. People may also talk about this as an offset so watch out for that.
This is my first time using Octave/MATLAB for this sort of project. I am also completely new to signal processing, so please excuse my lack of knowledge. The end goal of this project is to create an mfile which will be able to take a wav file which will be recorded from a microphone, add a level of noise distortion to it which will be specified by the user in increments, and also to add variable onset delay to either the right or left channel of the audio for the new wav file that will be generated.
edit 12:29AM 5/13/14
I have a more clear idea of what needs to happen now after discussion with partner on goals and equipment and now need to find out how to solve these blanks. The delay will most likely have to be between 10 and 300 ns max and the intensity of noise should be from 0 to 5 on a scale of silent to heavy static.
clear;
delay=input('Delay in ns: ');
noise=input('Level of distortion: );
[y,Fs,nbits]=wavread(filename);
generate some noise same length and sampling as file
[newy,Fs]=[y+y2,Fs];
shift over wave x many nanoseconds
wavwrite(newy,Fs,'newwave');
Any help with the current goal of combining signals OR if you could help with generating noise to overlay onto any size of .wav recording I would be extremely grateful.
Here's an example of how it might work. I've simplified the problem by limiting the delay to multiples of the sample period. For a 48kHz sample rate, the delay resolution is around 20us. This method is to first convert the delay to a number of samples and prepend it to the samples from the wave file. Second, the noise signal is generated of the same length and then it is added element wise to the first signal.
noiseLevel = input('Level of distortion: '); % between 0 and 1 - 0 means all signal
- 1 means all noise
delaySeconds = input('Delay in seconds: '); % in seconds
[y,fs,nbits] = wavread(filename);
% figure out how many samples to delay. Resolution is 1/fs.
delaySamples = round(delaySeconds * fs);
% signal length
signalLength = length(y) + delaySamples;
% generate noise samples
noiseSignal = gennoise(signalLength); % call your noise generation function.
% prepend zeros to the signal to delay.
delayedSignal = vertcat(zeros(delaySamples,1), y);
combinedSignal = noiseLevel*noiseSignal + (1-noiseLevel)*delayedSignal;
Couple of points:
Unless I'm doing my math wrong (entirely possible), a delay of 10 to 300 ns is not going to be detectable with typical 44 kHz audio sampling rates. You'd need to be in the MHz sampling rate range.
This solution assumes that your signal is one channel (mono). It shouldn't be too difficult to implement more channels using this format.
For adding noise, you can use randn and scale it based on your level of distortion. I'd suggest tinkering with the value that you multiply it by. Alternatively, you can use awgn to add white gaussian noise. I'm sure there are ways to add other kinds of noise, either in Fourier or time domain, but you can look into those.
If you want the noise during the delay, switch the order of the two.
A reminder that you can use sound(newy,Fs) to see if you like your result.
clear;
delay=input('Delay in ns: ');
noise=input('Level of distortion: );
[y,Fs,nbits]=wavread(filename);
% Add random noise to signal
newy = y + noise*0.1*randn(length(y),1);
% shift over wave x many nanoseconds
num = round(delay*1e-9*Fs); % Convert to number of zeros to add to front of data
newy = [zeros(num,1); newy]; % Pad with zeros
wavwrite(newy,Fs,'newwave');
Here is the context of the problem: I have a DTMF signal in wav format, I have to identify the number sequence it has encoded. I must do so using fast fourier transform in Matlab, implying that I read the wav file using wavread and to identify each number that is seperated by 40ms silence or more.
Here is my code so far:
[signal, fs] = wavread( 'C:\Temp\file.wav' ); % here, fs = 8000Hz
N = 512;
T = 1/fs;
L = length( signal )
samples = fs / 1000 * 40
windows = floor(L / samples)
t = (1:L)/fs;
figure(1), plot(t, signal);
Here is what the figure 1 looks like, that is the signal read from the wav:
How can I effectively split the signal into pieces so that I can then do an FFT on each of the 10 pieces seperately to decode the corresponding numbers?
I would recommend the following approach:
Find the envelope of the signal in the time domain (see Hilbert transform).
Smooth the envelope a bit.
Take the diff and find peaks to get the onsets of the tones.
Use the onsets to pick frames and find the spectrum using fft.
Find the index of the max in each of the spectrums and convert them to a frequency.
The tricky part in this is to get a robust onset detector in point 3. The peaks in the difference you pick, has to be of a certain size in order to qualify as on onset. If your tones are of varying strength this might pose a problem, but from your image of the time signal it doesn't seem like a problem.
Regards
This worked for me:
windowSize = 256;
nbWindows = floor(L / windowSize);
for i=1:nbWindows
coeffs = fft(signal((i-1)*windowSize+1:i*windowSize));
plot(abs(coeffs(1:N)));
waitforbuttonpress
end;
This way it is possible to shift the window until the end of the input signal
Basically I have a school project where I must create a new wave file from a given wave file. This new wave must be created in Matlab and the only difference is that a sine wave is to be mixed with the given wav file (not concatenated onto the end..). The sine wave is to be of 500hz.
My code so far is:
clear;
filename = 'C:\Documents and Settings\cmh0007\My Documents\rofl.wav';
[y, Fs, nbits, readinfo] = wavread(filename);
duration = numel(y) / Fs;
sinefs = 0:0.002:duration;
sinwave = 0.5*sin(2*pi*sinefs);
disp(size(y));
disp(size(sinwave));
newsignal = y + sinwave;
subplot(2,2,1), plot(y), title('Entire waveform');
subplot(2,2,3), plot(sinwave), title('sine waveform');
subplot(2,2,2), plot(newsignal), title('added waveform');
however, this code fails on creating the newsignal variable. The issue is that the two matrices are of different sizes due to the sampling rates differing between the two waves.
The output of the size calls are as follows:
797696 2
1 18089
Seeing as these files differ in sizes by a factor of ~44 I figured I could simply use the same sample from the sin wave 44 times over for each sample of the given wave file. However, because the difference is not exactly 44 I don't know if this is even an option.
Does anyone have any suggestions on how to go about mixing these two files?
Try changing 0.002 to 1/Fs. That way you'll have the same sampling rate. You should also choose only one of the stereo channels for y and do an appropriate transpose.
You might also consider changing the name sinefs to sinet or something as it represents the time parameter and not the sampling frequency.