Last active
July 23, 2021 15:44
-
-
Save SPLOpenData/0434dc9ce3b83f1c49d1d553c3f261e3 to your computer and use it in GitHub Desktop.
"The Pulse of COVID" This R script creates an audio 'visualization' of COVID data from the CDC.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#"The Pulse of COVID" | |
#This R script creates an audio 'visualization' of covid data from the CDC. | |
#Notes on what you'll hear: (1) New cases aggregated to week are scaled as the volume and new deaths as the pitch. | |
#(2) The addition of new data over time will affect the scaled output sound of code re-reun in the future. | |
library(RSocrata) #For downloading CDC data. | |
library(tuneR) #Making a .wav file | |
library(dplyr) | |
library(lubridate) #Date math --aggregating daily data into weekly data. | |
#Download data from the CDC. | |
df<-RSocrata::read.socrata("https://data.cdc.gov/Case-Surveillance/United-States-COVID-19-Cases-and-Deaths-by-State-o/9mfq-cb36/data") | |
#Get floor date for each week | |
df$week<-lubridate::floor_date( df$submission_date,unit = "week", week_start = getOption("lubridate.week.start", 7) | |
) | |
#Create summary of CDC data by week. | |
weekly_COVID<-df %>% select(week, new_case,new_death) %>% | |
group_by(week ) %>% | |
summarize(cases =sum(new_case),deaths =sum(new_death)) | |
#Scaling numbers to a high and low volume and pitch. | |
volume<-scales::rescale(weekly_COVID$cases, to=c(.05,1.8)) | |
pitch<-scales::rescale(weekly_COVID$deaths, to=c(30,100)) | |
#sample rate per second. | |
sr<-41000 | |
#define wavelengths | |
wavelength<-c(2109.89, 1991.47, 1879.69, 1774.2, 1674.62, 1580.63, 1491.91, 1408.18, 1329.14, 1254.55, 1184.13, 1117.67, 1054.94, 995.73, 939.85, 887.1, 837.31, 790.31, 745.96, 704.09, 664.57, 627.27, 592.07, 558.84, 527.47, 497.87, 469.92, 443.55, 418.65, 395.16, 372.98, 352.04, 332.29, 313.64, 296.03, 279.42, 263.74, 248.93, 234.96, 221.77, 209.33, 197.58, 186.49, 176.02, 166.14, 156.82, 148.02, 139.71, 131.87, 124.47, 117.48, 110.89, 104.66, 98.79, 93.24, 88.01, 83.07, 78.41, 74.01, 69.85, 65.93, 62.23, 58.74, 55.44, 52.33, 49.39, 46.62, 44.01, 41.54, 39.2, 37, 34.93, 32.97, 31.12, 29.37, 27.72, 26.17, 24.7, 23.31, 22, 20.77, 19.6, 18.5, 17.46, 16.48, 15.56, 14.69, 13.86, 13.08, 12.35, 11.66, 11, 10.38, 9.8, 9.25, 8.73, 8.24, 7.78, 7.34, 6.93, 6.54, 6.17, 5.83, 5.5, 5.19, 4.9, 4.63, 4.37) | |
#create a round wave can take wavelength as a function | |
snd_round_zag <-function(y){ | |
x<-seq(0, 1, 1/(as.integer(wavelength[y])/2)) | |
forml<- -x^2+x | |
cycle_round<-(c(forml,-forml)) | |
cycle_round<- scales::rescale(cycle_round, to= c(-1,1))*2^30 | |
z1<-seq(0, .25, 1/(as.integer(wavelength[y])/2)) | |
z2<-seq(.25, -.25, -1/(as.integer(wavelength[y])/2)) | |
z3<-seq(-.25, 0, 1/(as.integer(wavelength[y])/2)) | |
cycle_round<-(c(forml,-forml,z1,z2,z3)) | |
cycle_round<- scales::rescale(cycle_round, to= c(-1,1))*2^30 | |
result<-rep(cycle_round,length.out=sr) | |
return(result) | |
} | |
#Use scaled new weekly cases and deaths to modulate the amplitude and pitch. | |
sound_list<-list("") | |
for (i in 1:length(pitch)){ | |
sound_list[[i]]<-snd_round_zag(pitch[i])*volume[i] | |
} | |
year_of_covid<-unlist(sound_list) | |
#Convert to a wav file. | |
u<-Wave(year_of_covid, samp.rate=sr, bit=32) | |
#play result | |
tuneR::play(u) | |
#save audio file to working directory | |
writeWave(u, "a_year_plus_of_covid.wav", extensible = TRUE) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
View from twitter post: