Max-filter in matlab? - matlab

I like to find the envelope of an audio signal using matlab, i.e. the red signal in the picture. I have implemented a max-filter but the implementation is very slow. Does matlab have a built in max-filter or some other function that can calculate the envelope? (I have found a median filter but no max-filter)

There is no built-in max filter, but there are user generated functions at the MATLAB File Exchange. For example:
http://www.mathworks.com/matlabcentral/fileexchange/24902-envelope

Related

Masking Audio STFT Matlab

I want to implantation a masking time-frequency audio.
In first, I am using the function : S=spectrogram(x,window,noverlap,nfft) on Matlab, to extract the STFT of the noise+target signal (from WAV file). After that, I am forcing on some coefficients of the STFT(S variable) to be zero with decision of some threshold. But after doing ISTFT I get complex values ( not a real values like I am Expecting - like audio signal).
Can anyone explain where the problem is coming from? And what is the accepted solution to a problem of this kind?
Note:
If I were doing FFT and there doing manipulations on the signal, I would make sure that the signal has properties to be real in time, but how to keep the properties in the STFT plane?
Are you using the MATLAB function spectrogram() or stft()?
I think you should use stft() (because you can use istft() to go back to time domain).
Also whatever processing you do to the time-frequency domain, you should do the same processing to both positive frequencies and negative frequencies.

MATLAB filter code

I designed an FIR filter (highpass) in the FDAtool. Now I have a sine wave created using MATLAB code which I need to pass from the filter to monitor its performance.
To generate the filter design code I selected: File > generate MATLAB code > MAT-file.
Is this the correct way to do it?
After the code is generated, how can I use it with the sine wave?
Thanks in advance for any kind of help.
If it works, it is not wrong. You want to store the code in a .m file though. You can run the function then to create your filter object. You can also make it more dynamic if you want by editing the function (like add input arguments to let it work for other frequencies or sampling frequencies as well). To confirm it works as it should you can use freqz to se the digital frequency response. To filter a signal then, you can use filter.
yFilt = filter(Hd, y0);
where y0 is the original signal yFilt the filtered signal and Hd the filter object. You can try the filter with white noise as well. The frequency response for AWGN should be the same as the filter's frequency response.

Notch or Bandstop filter and preparing data for it

I am new to matlab and signal processing methods, but i am trying to use its filter properties over a set of data I have. I have a collection of amplitude values obtained at different timestamps. When this is plotted, I get a waveform with several peaks that I can identify. I then perform calculations to derive the time between each consecutive peak and I want to eliminate the rates that are around the range of 48-52peaks per second.
What would be the correct way to go about processing this data step by step? Would a bandstop or notch filter be better if I want to eliminate those frequencies and not attenuate it simply? I am completely lost in the parameters required to feed into the filters for this. Please help...
periodogram is OK, but I would suggest using pwelch instead. It makes a more reasonable PSD estimate and the default parameters are well thought out (Hann windows, 50% overlap of segments, etc.)
If what you want is to remove signals in a wide band (e.g. 48-52 Hz) equally, rather than a single and unchanging frequency, than a bandstop filter is ideal. For example:
fs = 2048;
y = rand(fs*8, 1);
[b,a] = ellip(4, 2, 40, [46 54]/(fs/2));
yy = filter(b,a,y);
This will use a 4th order elliptic bandstop filter to filter the random data variable 'y'. filtfilt.m is also a nice function; it applies the filter forwards and backwards so you get twice the filter action and none of the phase lag or dispersion.
I am currently doing something similar to what you are doing.
I am processing a lot of signals from the Inertial Measurement Unit and motor drives. They all are asynchronously obtained, i.e. they all have very different timestamp and also very different acquisition frequency.
First thing I did was to interpolate all signals data in order to have all signals with same timestamp. You can use the matlab function interp to do this.
After this, you will have all signals with same sample frequency and also timestamp, which will be good in further analysis.
Ok, another thing you can do to analyse the frequency of the peaks is to perform the fourier transform. For beginners i advice the use of periodogram function and not the fft function.
Imagine you signal is x and your sample frequency (after interpolation) is Fs.
You can now use the function periodogram available in matlab like this:
[P,f] = periodogram(x,[],[length(t)],Fs);
This will give the power vs frequency of your signal. After that you will be able to plot and take a look at the frequencies of your signal. In other words, you be able to see the frequencies of the signals that make your acquired signal.
Plot the data this way:
plot(f,P); or semilogy(f,P);
The second is the same thing as the first, but with a logarithmic scale.
After this analysis you can use the Filter Desing and Analysis Tool to design you filter. Just type fdatool in matlab and it will open the design window. Choose the filter type, the cut and pass frequencies and click in design. This tool is very intuitive.
After designing you can export the filter to workspace.
Finally you can use the filter you designed in your signal to see if its what you wanted.
Use the functions filter os filtfilt for this.
Search in the web of the matlab help for the functions I wrote to get more details.
There are a lot of examples availables too.
I hope I could help you.
Good luck.

How to generate a curve using matlab simulink

How to generate the curve of transfer function:
G0(s)=exp(-s)*[(2s+4)/(s^3+5s^2+6s+4)]
using MATLAB and Simulink?
What kind of curve do you mean? Bode plot? Nyquist plot? Step response?
Use the tf function to create your transfer function with the InputDelay parameter to represent the exp(-s), see Models with Time Delays in the documentation for more details.
Once you have your transfer function, you can use functions like bode, nyquist, step, impulse, etc... to generate your "curve" of choice.
All of this requires the Control System Toolbox.

Fitting distribution functions in Matlab

I am trying to use the stats toolbox to fit a distribution function. In my case, I already have the PMF (probability mass function, stored in an array) and wanted to fit it. But apparently, the toolbox can only take a vector of samples, from which a histogram is created.
Is there a way to pass it the PMF instead?
There are many functions in the Stats toolbox that can fit a bunch of data to a distribution. Say you wanted to fit your data (in an array) to be fit to a normal distribution, you'd use normfit like this (example taken from normfit doc)
data = normrnd(10,2,100,2);
[mu,sigma,muci,sigmaci] = normfit(data)
If the above doesn't answer you question, can you please include a snippet of code that succinctly describes what you are trying to do?