How to detect the fundamental frequency in a signal using wavelets? - wavelet

I have a noisy sine signal from which I want to extract the fundamental frequency of the sine. What should I use?
The signal is in the discrete time domain with a known sampling frequency. Actually I am trying to corroborate the answer I got by an FFT analysis so I cannot use a FFT.

Here is a piece of R code which implements continuous wavelet transform on some signal (using the biwavelet package).
The signal is composed of a sine wave with frequency 40 Hz and random noise.
The result is a spectrum showing that the frequency 40 Hz dominates in the whole signal.
signal.len <- 1024 # input param.
sin.freq <- 40 # input param.
# noise + 40 Hz sine wave
signal <- rnorm(signal.len) + sin(sin.freq*2*pi/signal.len * 1:signal.len)
library(biwavelet)
w <- wt(cbind(index(signal), signal)) # continuous wavelet transform
# rendering the results
par(mfrow=c(2,1))
plot.default(signal, type = "l", xaxs = "i") # the signal
plot.biwavelet(w, useRaster = TRUE) # spectrum of the signal

Related

How can i make 16000Hz sample from 44100Hz sample in a real-time stream?

I use portaudio in a Cpp work.
My signal model treats the only 16000Hz audio input and
When the First released my work, I don't need to use 44100 sample rate. It was just about 48000Hz microphone.
So I resampled my signal like 48000 -> 16000 -> 48000 with a simple decimation algorithm and linear interpolation.
But now I want to use a 44100 microphone. In real-time processing, My buffer is 256 points in 16000 Hz. So it is hard to find the input buffer size in 44100 Hz and downsample from 44100 to 16000.
When I used just decimation or average filter(https://github.com/mattdiamond/Recorderjs/issues/186), the output speech is higher then input and windowed sinc function interpolation makes a distortion.
is there any method to make 44100->16000 downsampling for realtime processing? please let me know...
thank you.
I had to implement a similar problem in the past, not for audio, but to simulate an asynchronism between a transmitte signal sampling frequency and a receiver sampling frequency.
This is how I will proceed:
Let us call T1 the sampling time duration of the incoming signal x: T1=1/44100 and
let us call T2 the sampling time duration of the signal to be generated y.
To calculate the value of the signal y[n*T2], select the two input values x[k*T1]and x[(k+1)*T2]
that surround the value to be calculated:
k*T1 <= n*T2 < (k+1)*T1
Then perform a linear interpolation from these two values. The interpolation factor must be recalculated for each sample.
If t = n*T2, a = k*T1 and b = (k+1)*T2, then
p = (x[b] - x[a])/T1
y[t] = p*(t-a) + x[a]
With a 44.1kHz frequency, x|a]and x[a+T1] should be rather well correlated, and the linear interpolation could be goood enough.
With the obtained quality is not good enough, you can interpolate the incoming signal with a fixed interpolation ratio,
for example 2, with a classical well defined good interpolation filter.
Then you can apply the previous procedure, with the help of the new calculated signal,
the sampling duration of which is T1/2.
If the incoming signal has some high frequencies, then, in order to avoid aliasing, you need to apply a low-pas filter to the incoming signal prior to the downsampling. Note that this is necessary even in your previous case 48kHz -> 16kHz

Compute FFT in frequency axis when signal is in rawData in Matlab

I have a signal of frequency 10 MHz sampled at 100 MS/sec. How to compute FFT in matlab in terms of frequency when my signal is in rawData (length of this rawData is 100000), also
what should be the optimum length of NFFT.(i.e., on what factor does NFFT depend)
why does my Amplitude (Y axis) change with NFFT
whats difference between NFFT, N and L. How to compute length of a signal
How to separate Noise and signal from a single signal (which is in rawData)
Here is my code,
t=(1:40);
f=10e6;
fs=100e6;
NFFT=1024;
y=abs(rawData(:1000,2));
X=abs(fft(y,NFFT));
f=[-fs/2:fs/NFFT:(fs/2-fs/NFFT)];
subplot(1,1,1);
semilogy(f(513:1024),X(513:1024));
axis([0 10e6 0 10]);
As you can find the corresponding frequencies in another post, I will just answer your other questions:
Including all your data is most of the time the best option. fft just truncates your input data to the requested length, which is probably not what you want. If you known the period of your input single, you can truncate it to include a whole number of periods. If you don't know it, a window (ex. Hanning) may be interesting.
If you change NFFT, you use more data in your fft calculation, which may change the amplitude for a given frequency slightly. You also calculate the amplitude at more frequencies between 0 and Fs/2 (half of the sampling frequency).
Question is not clear, please provide the definition of N and L.
It depends on your application. If the noise is at the same frequency as your signal, you are not able to separate it. Otherwise, you can a filter (ex. bandpass) to extract the frequencies of interest.

Length of FFT and IFFT

I have some signals which I add up to a larger signal, where each signal is located in a different frequency region.
Now, I perform the FFT operation on the big signal with FFTW and cut the concrete FFT bins (where the signals are located) out.
For example: The big signal is FFT transformed with 1024 points,
the sample rate of the signal is fs=200000.
I calculate the concrete bin positions for given start and stop frequencies in the following way:
tIndex.iStartPos = (int64_t) ((tFreqs.i64fstart) / (mSampleRate / uFFTLen));
and e.g. I get for the first signal to be cut out 16 bins.
Now I do the IFFT transformation again with FFTW and get the 16 complex values back (because I reserved the vector for 16 bins).
But when I compare the extracted signal with the original small signal in MATLAB, then I can see that the original signal (is a wav-File) has xxxxx data and my signal (which I saved as raw binary file) has only 16 complex values.
So how do I obtain the length of the IFFT operation to be correctly transformed? What is wrong here?
EDIT
The logic itself is split over 3 programs, each line is in a multithreaded environment. For that reason I post here some pseudo-code:
ReadWavFile(); //returns the signal data and the RIFF/FMT header information
CalculateFFT_using_CUFFTW(); //calculates FFT with user given parameters, like FFT length, polyphase factor, and applies polyphased window to reduce leakage effect
GetFFTData(); //copy/get FFT data from CUDA device
SendDataToSignalDetector(); //detects signals and returns center frequency and bandwith for each sigal
Freq2Index(); // calculates positions with the returned data from the signal detector
CutConcreteBins(position);
AddPaddingZeroToConcreteBins(); // adds zeros till next power of 2
ApplyPolyphaseAndWindow(); //appends the signal itself polyphase-factor times and applies polyphased window
PerformIFFT_using_FFTW();
NormalizeFFTData();
Save2BinaryFile();
-->Then analyse data in MATLAB (is at the moment in work).
If you have a real signal consisting of 1024 samples, the contribution from the 16 frequency bins of interest could be obtained by multiplying the frequency spectrum by a rectangular window then taking the IFFT. This essentially amounts to:
filling a buffer with zeros before and after the frequency bins of interest
copying the frequency bins of interest at the same locations in that buffer
if using a full-spectrum representation (if you are using fftw_plan_dft_1d(..., FFTW_BACKWARD,... for the inverse transform), computing the Hermitian symmetry for the upper half of the spectrum (or simply use a half-spectrum representation and perform the inverse transform through fftw_plan_dft_c2r_1d).
That said, you would get a better frequency decomposition by using specially designed filters instead of just using a rectangular window in the frequency domain.
The output length of the FT is equal to the input length. I don't know how you got to 16 bins; the FT of 1024 inputs is 1024 bins. Now for a real input (not complex) the 1024 bins will be mirrorwise identical around 512/513, so your FFT library may return only the lower 512 bins for a real input. Still, that's more than 16 bins.
You'll probably need to fill all 1024 bins when doing the IFFT, as it generally doesn't assume that its output will become a real signal. But that's just a matter of mirroring the lower 512 bins then.

locating frequencies in the spectrum after FFT [duplicate]

This question already has answers here:
How do I obtain the frequencies of each value in an FFT?
(5 answers)
Closed 8 years ago.
I've got 16-bit mono audio data in raw format, sampled at 48 KHz. I'm using Aquila C++ library to get the spectrum of it, since I need to perform EQ on it. Here's a code snippet:
Aquila::SampleType samples[512];
Aquila::SpectrumType spect;
std::shared_ptr<Aquila::Fft> fft;
('samples' is filled with audio)
fft = Aquila::FftFactory::getFft(512);
spect = fft->fft(samples);
So the audio data is split into 512 samples, and each piece is converted to frequency domain (FFT). I want to change the "magnitude" of e.g. 2KHz and to set the magnitude of all the frequencies beyond e.g. 10 KHz to 0 (low pass filter).
My only problem with this is that I don't know the frequency range of the spectrum generated by Aquila. I mean, I personally know that the sampling rate of the audio was 48 KHz, but Aquila FFT isn't told this value, it doesn't even need it to perform FFT. How can I determine to exactly which frequency each array entry is mapped to? E.g. spect[0] = 1 Hz, spect[10] = 126 Hz, spect[511] = 22.13 KHz etc.
As it turns out from the comments, FFT doesn't have to be explicitly told the sampling frequency used for sampling the audio signal. One another thing to watch for is that only half of the bins holds relevant information. The frequency of n'th bin in case of an 512-step FFT is:
freq = (n * sample_rate) / 512

FIR filter design: how to input sine wave form

I am currently taking a class in school and I have to code FIR/IIR filter in C/C++.
As an input to the filter, 2kHz sine wave with white noise is used. Then, by inputting the sine wave to the C/C++ code, I need to observe the clean sine wave output. It's all done in software level.
My problem is that I don't know how to deal with this input/output of sine wave. For example, I don't know what type of file format I can use or need to use, I don't know how to make the sine wave form and etc.
This might be a very trivial question, but I have no clue where to begin.
Does anyone have any experience in this type of question or have any tips?
Any help would be really appreciated.
Generating the sine wave at 2kHz means that you want to generate values over time that, when graphed, follow a sine wave. Pick an amplitude (you didn't mention one), and pick your sample rate. See the graph here (http://en.wikipedia.org/wiki/Sine_wave); you want values that when plotted follow the sine wave graphed in 2D with the X axis being time, and the Y axis being the amplitude of the value you are measuring.
amplitude (volts, degrees, pascals, milliamps, etc)
frequency (2kHz, that is 2000 sine waves/second)
sample rate (how many samples do you want per second)
Suppose you generate a file that has a time value and an amplitude measurement, which you would want to scale to your amplitude (more on this later). So a device might give an 8-bit or 16-bit digital reading which represents either an absolute, or logarithmic measurement against some scale.
struct sample
{
long usec; //microseconds (1/1,000,000 second)
short value; //many devices give a value between 0 and 255
}
Suppose you generate exactly 2000 samples/second. If you were actually measuring an external value, you would get the same value every time (see that?), which when graphed would look like a straight line.
So you want a sample rate higher than the frequency. Suppose you sample as 2x the frequency. Then you would see points 180deg off on the sine wave, which might be peaks, up or down slope, or where sine wave crosses zero. A sample rate 4x the frequency would show a sawtooth pattern. And as you increase the number of samples, your graph looks closer to the actual sine wave. This is similar to the pixelization you see in 8-bit game sprites.
How many samples for any given sine wave would you think would give you a good approximation of a sine wave? 8? 16? 100? 500? Suppose you sampled 1,000,000 times per second, then you would have 1,000,000/2,000 = 500 samples per sine wave.
pick your sample rate (500)
define your frequency (2000)
decide how long to record your samples (5 seconds?)
define your amplitude (device measures 0-255, but what is measured max?)
Here is code to generate some samples,
#define MAXJITTER (10)
#define MAXNOISE (20)
int
generate_samples( long duration, //duration in microseconds
int amplitude, //scaled peak measurement from device
int frequency, //Hz > 0
int samplerate ) //how many samples/second > 0
{
long ts; //timestamp in microseconds, usec
long sdelay; //sample delay in usec
if(frequency<1) frequency1=1; //avoid division by zero
if(samplerate<1) samplerate=1; //avoid division by zero
sdelay = 1000000/samplerate; //usec delay between each sample
sample m;
int jitter, noise; //introduce noise here
for( long ts=0; ts<duration; ts+=sdelay ) // //in usec (microseconds)
{
//jitter, sample not exactly sdelay
jitter = drand48()*MAXJITTER - (MAXJITTER/2); // +/-1/2 MAXJITTER
//noise is mismeasurement
noise = drand48()*MAXNOISE - (MAXNOISE/2); // +/-1/2 MAXNOISE
m.usec = ts + jitter;
//2PI in a full sine wave
float period = 2*PI * (ts*1.0/frequency);
m.value = sin( period );
//write m to file or save me to array/vector
}
return 0; //return number of samples, or sample array, etc
}
First generate some samples,
generate_samples( 5*1000000, 100, 2000, 2000*50 );
You could graph the samples generated as a view of the noisy signal.
The above certainly answers many of your questions about how to record measurements, and what format is typically used. And it shows how transit through the period of multiple sine waves, generate random samples with jitter and noise, and record samples over some time duration.
Building your filter is a second issue. Writing the code to emulate the filter(s) described below is left as an exercise, or a second question as you glean more understanding,
http://en.wikipedia.org/wiki/Finite_impulse_response
http://en.wikipedia.org/wiki/Infinite_impulse_response
The generated sample of the signal (above) would be fed into the code you write to build the filter. Expect that the output of the filter would be a new set of samples, perhaps with jitter, but expect that your filter would eliminate at least some of the noise. You would then be able to graph the samples produced by the filter.
You might consider that converting the samples into a comma delimited file would enable you to load them into excel and graph them. And it might help if you elucidated your electronics background, your trig knowledge, and how much you know about filters, etc.
Good luck!