I've been working on a proyect and I have to obtain the max value of a plot that I made in PlotlyJS, I need to obtain the frequency of a .wav file and print the musical note associated to that frequency.
http://samcarcagno.altervista.org/blog/basic-sound-processing-julia/
I have been following this post but this only gave you the spectrogram of the frequency. To obtain the fundamental frequency I changed the value of y.
plot(scatter(;x=freqArray/1000, y=p),
Layout(xaxis_title="Frecuencia (kHz)",
xaxis_zeroline=false,
xaxis_showline=true,
xaxis_mirror=true,
yaxis_title="Intensidad (dB)",
yaxis_zeroline=false,
yaxis_showline=true,
yaxis_mirror=true))
That's the plot
Please help me, I don't know how to obtain the frequency
You have two coupled vectors. p contains the value of the intensities, and freqArray contains the matching frequencies. Your plot is displaying a sequence of (x,y) points defined by (freqArray[i],p[i]) for all indices i.
You can use indmax(p) to return the index at which p has the maximal value. Then you can look up what the frequency is by indexing into freqArray at that index.
julia> p = rand(200);
freqArray = 5:5:1000;
julia> idx = indmax(p)
114
julia> p[idx] # this is the maximum value
0.9968329198539723
julia> freqArray[idx] # and this is its frequency
570
Related
Let's just say I have the following scatterplot:
set.seed(665544)
n <- 100
x <- cbind(
x=runif(10, 0, 5) + rnorm(n, sd=0.4),
y=runif(10, 0, 5) + rnorm(n, sd=0.4)
)
plot(x)
I want to divide this scatterplot into square cells of a specified size and then count how many points fall into each unique cell. This will essentially give me the local density value of that cell. What is the best way of doing this? Is there an R package that can help? Perhaps a 2D histogram method like in Matlab?
Quick clarifications:
1.) I'd like the function/method to take the following 3 arguments: dimensions of total area, dimensions of cell (OR number of cells), and the data. It would then perhaps output a matrix where each value corresponds to a cell's point count.
2.) Q: Why do you want to use this method to determine local density? Isn't this much easier:
library(dbscan)
pointdensity(x, eps = .1, type = "frequency")
A: This method calculates the local density around each point. Though easy, this definition of local density then makes it very difficult (optimization algorithms necessary) to assign new data in a way that it matches the local density distribution of the original data set.
Suppose I have some 2D data points, and using the Plots package in Julia, a 2D histogram can be easily plotted. My task is to define a function that maps between a data point to the frequency of data points of the bin to which that point belongs to. Are there any functions that serve well for this task?
For example, as in the following 2D histogram:
And I would like to define a function, such that when I input an arbitrary data points that is within the domain of this histogram, the function will output the frequency of the corresponding bin. In the image above, when I input (0.1, 0.1), the function should output, say, 375 (I suppose the brightest grid there represents the frequency of 375). Are there any convenient functions in Julia to achieve the aforementioned task?
Edit:
using Plots
gr()
histogram2d(randn(10000), randn(10000), nbins=20)
A histogram is created from 10000 2D data points generated from standard normal distribution. Is there any function in Julia to input a 2D point and output the frequency of the bin to which the point belongs to? It is possible to write one myself by creating arrays and bins and counting the number of elements in the bin of an inputted data point but this will be the tedious way.
I'm not 100% sure whether this is what StatsPlots is doing, but one approach could be to use StatsBase's histogram which works for N dimensions:
using StatsBase, StatsPlots, Distributions
# Example data
data = (randn(10_000), randn(10_000))
# Plot StatsPlots 2D histogram
histogram2d(data)
# Fit a histogram with StatsBase
h = fit(Histogram, data)
x = searchsortedfirst(h.edges[1], 0.1) # returns 10
y = searchsortedfirst(h.edges[2], 0.1) # returns 11
h.weights[x, y] # returns 243
# Or as a function
function get_freq(h, xval, yval)
x = searchsortedfirst(h.edges[1], xval)
y = searchsortedfirst(h.edges[2], yval)
h.weights[x, y]
end
get_freq(h, 1.4, 0.6) # returns 32
Hi ~ I'm try to make graph which has sample mean on x-axis and
relative frequency(?) on y-axis
to make sure i will give example!
for example when i pick 1sample from c(1,2,3,4,5)
the possible result will be 1 and 2 and 3 and 4 and5
in that case the relative frequency is 1/5 each !!
so in this case my graph will show 1,2,3,4,5 on x-axis
0.2 for y -axis (because they are same in 1/5)
and if i pick 2sample from c(1,2,3,4,5) case would be
(1,2) and (1,3), (1,4), (1,5) (2,3)..... and so on (total 10cases)
so sample mean would be (1+2)/2=1.5 .. (1+3)/2=2 .... etc
so in this case x value will be 1.5, 2 ... etc and y value will
1/10 1/10 ...
so, My question is, is histogram is appropriate for this graph??
i want to plot which have sample mean on x -axis, relative frequency on y-axis
and make a line that connect a dot
sorry for too long question
thanks for reading!!
Yes, it's entirely appropriate to plot a histogram of sample means. This is an example of a sampling distribution.
To do this, you would create an object that contains the sample means, and then just plot a histogram of that object as you would with any other histogram. The value of the sample means would be on the x axis, and frequency or relative frequency on the y axis. You would have to choose an appropriate bin number and breaks vector for your purpose, but it's the same as any other histogram.
So, I have two matrices, let's say:
set.seed(11)
a<-matrix(rnorm(10000),ncol=100)
colnames(a)<-(c(1:100))
set.seed(31)
b<-matrix(rnorm(10000),ncol=100)
colnames(b)<-colnames(a)
I want to create a scatter plot where each point will have in:
x axis -> the value of (i,j) from matrix a
y axis -> the value for the same pair (i,j) from matrix b
Somewhat is more difficult for me than it seems..
Converting them to vectors will do what you need. Every combination i,j from a will match the same combination of i,j from b:
plot(as.vector(a), as.vector(b))
I am trying to convey the concentration of lines in 2D space by showing the number of crossings through each pixel in a grid. I am picturing something similar to a density plot, but with more intuitive units. I was drawn to the spatstat package and its line segment class (psp) as it allows you to define line segments by their end points and incorporate the entire line in calculations. However, I'm struggling to find the right combination of functions to tally these counts and would appreciate any suggestions.
As shown in the example below with 50 lines, the density function produces values in (0,140), the pixellate function tallies the total length through each pixel and takes values in (0, 0.04), and as.mask produces a binary indictor of whether a line went through each pixel. I'm hoping to see something where the scale takes integer values, say 0..10.
require(spatstat)
set.seed(1234)
numLines = 50
# define line segments
L = psp(runif(numLines),runif(numLines),runif(numLines),runif(numLines), window=owin())
# image with 2-dimensional kernel density estimate
D = density.psp(L, sigma=0.03)
# image with total length of lines through each pixel
P = pixellate.psp(L)
# binary mask giving whether a line went through a pixel
B = as.mask.psp(L)
par(mfrow=c(2,2), mar=c(2,2,2,2))
plot(L, main="L")
plot(D, main="density.psp(L)")
plot(P, main="pixellate.psp(L)")
plot(B, main="as.mask.psp(L)")
The pixellate.psp function allows you to optionally specify weights to use in the calculation. I considered trying to manipulate this to normalize the pixels to take a count of one for each crossing, but the weight is applied uniquely to each line (and not specific to the line/pixel pair). I also considered calculating a binary mask for each line and adding the results, but it seems like there should be an easier way. I know that you can sample points along a line, and then do a count of the points by pixel. However, I am concerned about getting the sampling right so that there is one and only one point per line crossing of a pixel.
Is there is a straight-forward way to do this in R? Otherwise would this be an appropriate suggestion for a future package enhancement? Is this more easily accomplished in another language such as python or matlab?
The example above and my testing has been with spatstat 1.40-0, R 3.1.2, on x86_64-w64-mingw32.
You are absolutely right that this is something to put in as a future enhancement. It will be done in one of the next versions of spatstat. It will probably be an option in pixellate.psp to count the number of crossing lines rather than measure the total length.
For now you have to do something a bit convoluted as e.g:
require(spatstat)
set.seed(1234)
numLines = 50
# define line segments
L <- psp(runif(numLines),runif(numLines),runif(numLines),runif(numLines), window=owin())
# split into individual lines and use as.mask.psp on each
masklist <- lapply(1:nsegments(L), function(i) as.mask.psp(L[i]))
# convert to 0-1 image for easy addition
imlist <- lapply(masklist, as.im.owin, na.replace = 0)
rslt <- Reduce("+", imlist)
# plot
plot(rslt, main = "")