-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWavelength-Frequency.R
93 lines (76 loc) · 2.59 KB
/
Wavelength-Frequency.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#
# Relation between wavelength and frequency
#
# https://nl.wikipedia.org/wiki/Geluidssnelheid#Geluidssnelheid_in_lucht
# https://en.wikipedia.org/wiki/Speed_of_sound
#
# Antoine van Kampen
#
library(ggplot2)
rm(list=ls())
T1 = 20 # temperature in Celsius
c = 20 * sqrt (273 + T1) # speed of sound (m/s)
print (c)
#c=lambda*freq [m] * [1/s])
f = seq(from=20, to=25000, by= 10) #frequency
l = c/f #lambda
fl1 = data.frame(cbind(f,l))
colnames(fl1) <- c('Freq','Lambda')
fl1
#make second curve at different temperature
#In the log-log plot the effect of Celsius cannot be distinguished.
T2 = 100 # temperature in Celsius
c = 20 * sqrt (273 + T2) # speed of sound (m/s)
print (c)
#c=lambda*freq [m] * [1/s])
f = seq(from=10, to=25000, by= 10) #frequency
l = c/f #lambda
fl2 = data.frame(cbind(f,l))
colnames(fl2) <- c('Freq','Lambda')
fl2
#Construct decades
df <- data.frame(matrix(vector(), 0, 4), stringsAsFactors=F)
CNames <- c('f_low','f_center','f_high','BW')
fc <- 1000
MultiplicationFactor <- 10
s <- seq(from=-3,to=1,by=1)
for (i in s) {
f <- fc * MultiplicationFactor^i
fl <- f / sqrt(MultiplicationFactor)
fh <- f * sqrt(MultiplicationFactor)
BW <- 100*(fh-fl)/f
r <- round(c(fl,f,fh,BW),1)
df <- rbind(df, setNames(r,names(df)))
}
colnames(df) <- CNames
df
freq = c()
for (i in 1:dim(df)[1]) {
FreqStart = df$f_center[i]
FreqInterval = df$f_center[i]
s = seq(from=FreqStart,to=MultiplicationFactor*FreqStart,by=FreqInterval)
freq=c(freq,s)
}
freq = unique(freq) #remove the frequencies that occur twice
df2 = data.frame(freq=as.integer(freq))
df2 = data.frame(df2[which(df2$freq>=10 & df2$freq <=30000),])
colnames(df2) <- c("freq")
df2
ggplot(NULL, aes(x=Freq,y=Lambda))+
geom_point(aes(color=factor(T1)), data=fl1,cex=0.3)+
geom_point(aes(color=factor(T2)),data=fl2,size=0.3)+
geom_vline(xintercept=df2$freq, linetype="solid",
color = "gray", size=0.5)+
labs(title="Wavelength vs Frequency", x ="Frequency (Hz)", y = "Wavelength (m)") +
theme(plot.title = element_text(hjust = 0.5)) +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.line = element_line(colour = "black")) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
scale_x_continuous(trans='log10',labels = df2$freq, breaks = df2$freq) +
scale_y_continuous(trans='log10')+
labs(color="Temperature")+
theme(legend.title = element_text(face = "bold"))+
theme(legend.key = element_rect(fill = "white"))+
guides(color = guide_legend(override.aes = list(size = 5)))